Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我可以在左sql函数中为整数表达式使用变量吗_Sql_Sql Server_Coldfusion - Fatal编程技术网

我可以在左sql函数中为整数表达式使用变量吗

我可以在左sql函数中为整数表达式使用变量吗,sql,sql-server,coldfusion,Sql,Sql Server,Coldfusion,我有以下疑问: SELECT top 2500 * FROM table a LEFT JOIN table b ON a.employee_id = b.employee_id WHERE left(a.employee_rc,6) IN ( SELECT employeeID, access FROM accesslist WHERE em

我有以下疑问:

    SELECT top 2500 *
    FROM  table a
    LEFT JOIN table b
    ON a.employee_id = b.employee_id
    WHERE left(a.employee_rc,6) IN 
           (
             SELECT employeeID, access 
             FROM   accesslist 
             WHERE  employeeID = '#client.id#' 
           ) 
where
子句中的子选择可以返回一个或多个访问值,例如:

  • js1234 BLKHSA
  • js1234 HDF48R7
  • js1234 BLN6

在主
where
子句中,我需要能够将整数表达式从6更改为5或4或7,具体取决于子选择中返回的值的长度。如果这是正确的方法,我就不知所措了。我尝试过使用OR语句,但它确实减慢了查询速度

尝试改用
存在

   SELECT top 2500 *
   FROM table a LEFT JOIN
        table b
        ON a.employee_id = b.employee_id
   WHERE EXISTS (Select 1
                 FROM accesslist
                 WHERE employeeID = '#client.id#' and
                       a.employee_rc like concat(employeeID, '%') 
                ) ;

我不明白你最初的查询是如何工作的。子查询返回两列,这在SQL中通常不允许用于

中的
将子查询移动到联接:

SELECT TOP 2500 *
    FROM table a
        LEFT JOIN table b ON a.employee_id = b.employee_id
        LEFT JOIN accesslist al ON al.access LIKE concat('%', a.employee_id)
    WHERE al.employeeID = '#client.id#'

像Gordon一样,我不太了解您的查询是如何工作的,所以我不太确定是否应该是匹配的
access
employeeID

此构造将使您能够执行您所说的要执行的操作,使整数值依赖于子查询中的某些内容。这只是一般的想法,细节由你决定

select field1, field2
, case when subqueryField1 = 'fred' then 1
when subqueryField1 = 'barney' then 2
else 3 end integerValue

from table1 t1 join (
select idField subqueryField1, etc
from whereever ) t2 on t1.idFeld = t2.idField
where whatever

此外,您的查询中有几件事值得怀疑。首先,没有ORDERBY子句的TOPN查询不会告诉数据库要返回哪些记录。其次,2500行是返回ColdFusion的大量数据。你确定你需要全部吗?第三,选择*而不是只选择所需的字段会降低性能。如果你认为你需要每一个领域,再想想。因为员工ID总是匹配的,所以您不需要两个ID。

这很有效。我没有提交完整的查询,因为我不想更改所有的名称..等等。谢谢,这很有效,明白了。我通常只选择我需要的字段,但出于提问的目的,我只选择*。谢谢你的提示,我将继续使用它们。