使用SQL Server 2008中的Case语句获取状态

时间:2014-10-29 17:41:24

标签: sql-server-2008

我是新的DBA我创建了一个库存检查存储过程。我需要检查我的过时和库存现有列,并将结果影响我的客户表上的状态列。以下是我使用的代码示例。

       INSERT INTO dbo.tblCSSCustDeferred_TestNew
       ([CONTACTTYPE]
       ,[CONTACTID]
       ,[TITLE]
       ,[LASTNAME]
      ,[FIRSTNAME]
      ,[MINIT]
      ,[ADDR1]
      ,[ADDR2]
      ,[CITY]
      ,[STATE]
      ,[ZIP]
      ,[QTY]
      ,[OPID]
      ,[BENESET]
      ,[RIDEROPTION]
      ,[SEQNUM]
      ,[LOB]
      ,[FORMIDNUM]
      ,[FORMDESCR]
      ,[ENTITYNAME]
      ,[ATTN]
      ,[EMPGRPID]
      ,[NETWORKCODE]
      ,[BRAND]
      ,[FormsMgmtNum]
      ,[FormsMgmtDesc]
      ,[Electronic]
      ,[LetterHead]
      ,[Envelope]
      ,[LANGUAGE]
      ,[JURISDICTION]
      ,[EFFECTIVE DATE]
      ,[Status])

 select  [CONTACTTYPE]
      ,[CONTACTID]
      ,[TITLE]
      ,[LASTNAME]
      ,[FIRSTNAME]
      ,[MINIT]
      ,[ADDR1]
      ,[ADDR2]
      ,[CITY]
      ,[STATE]
      ,[ZIP]
      ,[QTY]
      ,[OPID]
      ,[BENESET]
      ,[RIDEROPTION]
      ,[SEQNUM]
      ,[LOB]
      ,[FORMIDNUM]
      ,[FORMDESCR]
      ,[ENTITYNAME]
      ,[ATTN]
      ,[EMPGRPID]
      ,[NETWORKCODE]
      ,[BRAND]
      ,[FormsMgmtNum]
      ,[FormsMgmtDesc]
      ,[Electronic]
      ,[LetterHead]
      ,[Envelope]
      ,[LANGUAGE]
      ,[JURISDICTION]
      ,[EFFECTIVE DATE]
         CASE  
         WHEN tsg.Obsolete = 'N' THEN 'OOS' 
         When tsg.[Inventory on Hand] = '0' THEN 'OOS'     
         else 'FulFilled'                 
       END 
       CASE   
         WHEN tsg.Obsolete = 'Y' THEN 'N/A' 
         When tsg.[Inventory on Hand] > 0 THEN 'Completed'     
         else 'OOS'               
       END   
         ,[Status]    
      FROM [CSSFulfillment].[dbo].[tblCSSCustUpdateTEST] cust left join
        [CSSFulfillment].[dbo].[tmpTSGInventoryImport] tsg on
        cust.FORMIDNUM = tsg.[Item ID]
        END

我的问题是这个查询抛出了一个我找不到的语法错误。这是错误信息;

  

Msg 156,Level 15,State 1,Line 68关键字' CASE'附近的语法不正确。

我已经厌倦了在线查找,但没有了。真的希望有人可以倾斜眼球并帮助我。

提前感谢您提供任何帮助。

1 个答案:

答案 0 :(得分:0)

字段和案例陈述之间缺少逗号....

更新了答案。

只需运行select语句,看看是否有结果。 然后尝试插入表格。请查看a,b列并替换为您想要的名称。

INSERT INTO dbo.tblCSSCustDeferred_TestNew
       ([CONTACTTYPE]
       ,[CONTACTID]
       ,[TITLE]
       ,[LASTNAME]
      ,[FIRSTNAME]
      ,[MINIT]
      ,[ADDR1]
      ,[ADDR2]
      ,[CITY]
      ,[STATE]
      ,[ZIP]
      ,[QTY]
      ,[OPID]
      ,[BENESET]
      ,[RIDEROPTION]
      ,[SEQNUM]
      ,[LOB]
      ,[FORMIDNUM]
      ,[FORMDESCR]
      ,[ENTITYNAME]
      ,[ATTN]
      ,[EMPGRPID]
      ,[NETWORKCODE]
      ,[BRAND]
      ,[FormsMgmtNum]
      ,[FormsMgmtDesc]
      ,[Electronic]
      ,[LetterHead]
      ,[Envelope]
      ,[LANGUAGE]
      ,[JURISDICTION]
      ,[EFFECTIVE DATE], a, b -- replace a and b with your column
      ,[Status])

select  [CONTACTTYPE]
      ,[CONTACTID]
      ,[TITLE]
      ,[LASTNAME]
      ,[FIRSTNAME]
      ,[MINIT]
      ,[ADDR1]
      ,[ADDR2]
      ,[CITY]
      ,[STATE]
      ,[ZIP]
      ,[QTY]
      ,[OPID]
      ,[BENESET]
      ,[RIDEROPTION]
      ,[SEQNUM]
      ,[LOB]
      ,[FORMIDNUM]
      ,[FORMDESCR]
      ,[ENTITYNAME]
      ,[ATTN]
      ,[EMPGRPID]
      ,[NETWORKCODE]
      ,[BRAND]
      ,[FormsMgmtNum]
      ,[FormsMgmtDesc]
      ,[Electronic]
      ,[LetterHead]
      ,[Envelope]
      ,[LANGUAGE]
      ,[JURISDICTION]
      ,[EFFECTIVE DATE]
      ,   CASE  
         WHEN tsg.Obsolete = 'N' THEN 'OOS' 
         When tsg.[Inventory on Hand] = '0' THEN 'OOS'     
         else 'FulFilled'                 
       END 
      , CASE   
         WHEN tsg.Obsolete = 'Y' THEN 'N/A' 
         When tsg.[Inventory on Hand] > 0 THEN 'Completed'     
         else 'OOS'               
       END   
         ,[Status]    
      FROM [CSSFulfillment].[dbo].[tblCSSCustUpdateTEST] cust left join
        [CSSFulfillment].[dbo].[tmpTSGInventoryImport] tsg on
        cust.FORMIDNUM = tsg.[Item ID]