结合MSSQL参数SQL于一体

时间:2016-03-29 15:28:22

标签: sql-server

我使用MSSQL 2008 我想结合#34;更多参数"进入相同的选择与不同的结果
我无法在运行时修改查询 表1 - 客户ID_PK,名称
表2 - 详情ID_PK,IDCLIENT,DETAIL

情况a 我需要所有客户的信息

SELECT C.IDCLIENT, C.NAME  
from CLIENTI C  
LEFT JOIN DETAILS D ON D.IDCLIENT = C.ID_PK
WHERE ... <where clause>

情况b
我需要来自特定IDCLIENT的信息

SELECT C.IDCLIENT, C.NAME  
from CLIENTI C  
LEFT JOIN DETAILS D ON D.IDCLIENT = C.ID_PK  
WHERE C.ID_PK = :AID  
AND ...<where clause>  

情况c
我需要来自一系列客户的信息(我将这些参数作为字符串传递)

SELECT C.IDCLIENT, C.NAME  
from CLIENTI C  
LEFT JOIN DETAILS D ON D.IDCLIENT = C.ID_PK  
WHERE C.ID_PK IN (:AID_STRING)  
AND ...<where clause>  

我如何制作这种参数组合?
勒兹

1 个答案:

答案 0 :(得分:0)

尝试这样的事情

SELECT C.IDCLIENT, C.NAME  
from CLIENTI C  
LEFT JOIN DETAILS D ON D.IDCLIENT = C.ID_PK
WHERE  (C.ID_PK = @AID or @AID is NULL)
... <where clause>

如果您不想@AID参数,请将其保留为NULL