这是表的示例:

     acIdent   |  acSubject          | acCode
     200.2.013 |                     | F-202
     200.2.013 | Minsk traktor works | F202.GRS


这是我试图做的:

     declare @subj varchar(30)

     set @subj = 'Minsk traktor works'

     select acIdent, acSubject, acCode
     from tHE_SetItemExtItemSubj where acIdent = '200.2.013' and
     acSubject = (
            case
            when @subj = acSubject then @subj
            else '' end
         )


我的目标是只获得一张唱片。我的示例返回两条记录。
如果表中存在@subj,则返回acCode-F202.GRS,否则返回F-202。

帮助请

最佳答案

如果我对您的理解正确,那么您正在寻找以下内容:

 declare @subj varchar(30)

 set @subj = 'Minsk traktor works'

 select top(1) acIdent, acSubject, acCode
 from tHE_SetItemExtItemSubj
 where acIdent = '200.2.013'
   and (acSubject = @subj or acSubject = '')
 order by len(acSubject) desc

关于sql-server - SQL在where子句中使用CASE,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52161687/

10-15 21:05