3

我正在尝试在 tsql 中使用案例中的案例来执行选择语句。首先是基于 SearchField 是什么。接下来我需要根据SearchOper来做。

    declare @searchField varchar(50)  
    declare @searchString varchar(50)
    declare @searchOper varchar(50)

    case @searchField
     when 'CompanyName' then
        case @searchOper
          when 'eq' then
            select * from tbl1 where CompanyName = @searchString
         when 'ne' then 
           select * from tbl1 where CompanyName <> @searchString
         end
     when 'StoreNum' then
        case @searchOper
          when 'eq' then
             select * from tbl1 where StoreNum = @searchString
          when  'ne' then
             select * from tbl1 where StoreNum <> @searchString
        end 
     end 

请注意,我要做的是在 case 语句中进行选择。

我收到一条消息,提示关键字“case”附近的语法不正确。

4

4 回答 4

5

根据您的示例,您可能想要使用 IF 语句(这是 ELSE 的示例,但您也可以多次使用 IF 和 ELSE IF):

declare @searchField varchar(50)  
declare @searchString varchar(50)
declare @searchOper varchar(50)

IF @searchField='CompanyName'
BEGIN
    IF @searchOper='eq'
    BEGIN
        select * from tbl1 where CompanyName = @searchString
    END
    ELSE
    BEGIN
        select * from tbl1 where CompanyName <> @searchString
    END
END
ELSE 
BEGIN
    IF @searchOper='eq'
    BEGIN
        select * from tbl1 where StoreNum = @searchString
    END
    ELSE
        select * from tbl1 where StoreNum <> @searchString
    END
END

CASE 非常相似,但更适用于内联语句(即 SELECT CASE 1=1 THEN 'a' ELSE 'b' END)。

于 2012-10-09T02:30:59.627 回答
1
declare @searchField varchar(50)  
declare @searchString varchar(50)
declare @searchOper varchar(50)

 select *
 from tbl1
 where (@searchField <> 'companyName'
       or
       @searchOper = 'eq' and CompanyName = @searchString
       or
       @searchOper = 'ne' and CompanyName <> @searchString)
   AND (@searchField <> 'StoreNum'
       or
       @searchOper = 'eq' and CompanyName = @searchString
       or
       @searchOper = 'ne' and CompanyName <> @searchString)
 option (recompile)

这是编写它的一种方法,它应该可以正常运行。您拥有的是www.sommarskog.se/dyn-search.html的变体,但OPTION (RECOMPILE)每次仍应生成最佳计划。

于 2012-10-09T02:50:44.873 回答
1
declare @searchField varchar(50)  
declare @searchString varchar(50)
declare @searchOper varchar(50)

SELECT
    (case @searchField
     when 'CompanyName' then
        (case @searchOper
          when 'eq' then
            (select * from tbl1 where CompanyName = @searchString)
         when 'ne' then 
           (select * from tbl1 where CompanyName <> @searchString)
         end
         )
     when 'StoreNum' then
        (case @searchOper
          when 'eq' then
             (select * from tbl1 where StoreNum = @searchString)
          when  'ne' then
            ( select * from tbl1 where StoreNum <> @searchString)
        end 
        )
     end 
) a

我懒得构建tbl1,改为getdate

declare @searchField varchar(50)  
declare @searchString varchar(50)
declare @searchOper varchar(50)

SET @searchField = 'CompanyName'
SET @searchOper = 'eq'
SELECT
    (case @searchField
     when 'CompanyName' then
        (case @searchOper
          when 'eq' then
            (select getdate())
         when 'ne' then 
           (select getdate())
         end
         )
     when 'StoreNum' then
        (case @searchOper
          when 'eq' then
             (select getdate())
          when  'ne' then
            ( select getdate())
        end 
        )
     end 
) a
于 2012-10-09T06:26:03.433 回答
0

根据 MSDN,

“CASE 表达式不能用于控制 Transact-SQL 语句、语句块、用户定义函数和存储过程的执行流程。”

参考这里

于 2012-10-09T06:47:43.230 回答