动态where子句在一个' AND'存储过程

时间:2014-07-18 14:36:06

标签: sql sql-server stored-procedures if-statement case

我有一个非常大的存储过程,我无法分享,但我只是在实现动态where子句时遇到了问题,该子句已经有7个AND。在WHERE子句的最后一个'And'中,我需要检查传递给存储过程的参数并相应地构造'And'。基本上,用户可以将买方或卖方或空字符串(表示同时使用两者)传递给存储过程。需要的算法:

SELECT blah blah blah 
FROM multiple joins, left outer joins blah
WHERE 
(1st clause) AND
(2nd clause) AND 
(3rd clause) AND 
(4th clause) AND 
(5th clause) AND 
(6th clause) AND 
(7th clause) AND
(need to check paramater passed in and fill in this and accordingly....
Basically in JS terms:
if (parameter ='buyer'){
 B.S_FNAME LIKE '%' + @Name + '%' OR 
 B.S_SNAME LIKE '%' + @Name + '%'
}
else if (parameter = 'seller'){
 S.S_FNAME LIKE '%' + @Name + '%' OR
 S.S_SNAME LIKE '%' + @Name + '%'
}
else{
 B.S_FNAME LIKE '%' + @Name + '%' OR 
 S.S_FNAME LIKE '%' + @Name + '%' OR 
 B.S_SNAME LIKE '%' + @Name + '%' OR 
 S.S_SNAME LIKE '%' + @Name + '%'
}
 ) 

我的尝试如下:@CliType是传入的参数:

AND (CASE 
        WHEN @CliType  = 'Buyer' THEN  
        B.S_FNAME LIKE '%' + @Name + '%' OR 
        B.S_SNAME LIKE '%' + @Name + '%'
        WHEN @CliType  = 'Seller' THEN 
        S.S_FNAME LIKE '%' + @Name + '%' OR
        S.S_SNAME LIKE '%' + @Name + '%'
        WHEN @CliType = '' THEN 
        B.S_FNAME LIKE '%' + @Name + '%' OR 
        S.S_FNAME LIKE '%' + @Name + '%' OR 
        B.S_SNAME LIKE '%' + @Name + '%' OR 
        S.S_SNAME LIKE '%' + @Name + '%'
        END)

但是这给了我第一个LIKE和第二个WHEN下的错误行。我正在使用sql server 2008并且没有多少经验。任何帮助表示赞赏

3 个答案:

答案 0 :(得分:2)

请试试这个,希望它有用。

DECLARE @CliType VARCHAR(100);

DECLARE @Name VARCHAR(100);

DECLARE @TableBuyer TABLE (ID INT,
                          S_FNAME VARCHAR(100),
                          S_SNAME VARCHAR(100));

DECLARE @TableSeller TABLE (ID INT,
                           S_FNAME VARCHAR(100),
                           S_SNAME VARCHAR(100));

--Assign your input parameters here
SET @CliType = 'Buyer' --NULL
SET @Name = 'Daniella'

INSERT INTO @TableBuyer
SELECT '1','Bryan', 'Greenberg' UNION ALL
SELECT '2','Channing', 'Tatum' UNION ALL
SELECT '3','Paul', 'William' UNION ALL
SELECT '4','Eric', 'Bana' UNION ALL
SELECT '5','James', 'Lafferty' UNION ALL
SELECT '6','Wentworth', 'Miller'

INSERT INTO @TableSeller
SELECT '1','Dianna', 'Agron' UNION ALL
SELECT '2','Malin', 'Akerman' UNION ALL
SELECT '3','Christina', 'Aguilera' UNION ALL
SELECT '4','Jessica', 'Alba' UNION ALL
SELECT '5','Krista', 'Allen' UNION ALL
SELECT '6','Daniella', 'Alonso'


SELECT b.ID,b.S_FNAME,b.S_SNAME,s.ID,s.S_FNAME,s.S_SNAME
FROM @TableBuyer b
JOIN @TableSeller s
ON b.ID=s.ID
WHERE (@CliType = 'Buyer'
 AND (B.S_FNAME LIKE '%' + @Name + '%'
   OR B.S_SNAME LIKE '%' + @Name + '%'))
  OR (@CliType = 'Seller'
 AND (S.S_FNAME LIKE '%' + @Name + '%'
   OR S.S_SNAME LIKE '%' + @Name + '%'))
  OR (ISNULL(@CliType, '') = ''
 AND (B.S_FNAME LIKE '%' + @Name + '%'
   OR S.S_FNAME LIKE '%' + @Name + '%'
   OR B.S_SNAME LIKE '%' + @Name + '%'
   OR S.S_SNAME LIKE '%' + @Name + '%'));

答案 1 :(得分:0)

不是Case Statement的工作原理。基本思想是对您要检查的每个条件使用类似的语法。

AND (CASE 
    WHEN @CliType  = 'Buyer' THEN  
    B.S_FNAME WHEN @CliType  = 'Seller' THEN 
    S.S_FNAME WHEN @CliType = '' THEN 
    B.S_FNAME END LIKE '%' + @Name + '%' )
OR..

答案 2 :(得分:0)

您可以解析参数并构建一个插入select语句的字符串变量。

if  @CliType  = 'Buyer' 
  THEN @WhereVariable = B.S_FNAME LIKE '''%''' + @Name + '''%''' OR...'
else if @CliType = 'Seller' then ...

然后只需将其插入最终成为完整查询的字符串中,然后使用sp_executesql

执行该字符串