在变量上使用多个值

时间:2013-10-15 14:56:16

标签: sql-server tsql stored-procedures

我正在创建一个存储过程,我需要某种变量包含多个值。我尝试过使用表变量,但这不是我想要的。原因是因为我必须检查select查询中的多个值,然后对这些值使用if语句。

我需要变量的代码是:

If @SplitVal = 1
Begin
    Insert into SpecDetailsExt (SrecId, SplitVal, Acknowledged,Required, [Drawings Produced]) 
    Select DISTINCT SpecDetails.SrecId, '1', @MoveToOrder, @RequiredDate, @Drawings
    FROM   SpecDetails INNER JOIN Products ON SpecDetails.ProductRef = Products.CDF_Code_Tx 
    WHERE (SpecDetails.QuoteNo = @Qno) 
End
ELSE
Begin
    Insert into SpecDetailsExt (SrecId, SplitVal, Acknowledged, Required, [Drawings Produced])
    Select DISTINCT SpecDetails.SrecId, '1', @MoveToOrder, @RequiredDate, @Drawings 
    FROM   SpecDetails INNER JOIN Products ON SpecDetails.ProductRef = Products.CDF_Code_Tx 
    WHERE (SpecDetails.QuoteNo = @Qno)

    Insert into SpecDetailsExt (SrecId, SplitVal, Acknowledged, Required, [Drawings Produced]) 
    Select DISTINCT SpecDetails.SrecId, '2', @MoveToOrder, @RequiredDate, @Drawings
FROM   SpecDetails 
          INNER JOIN Products ON SpecDetails.ProductRef = Products.CDF_Code_Tx 
    WHERE (SpecDetails.QuoteNo = @Qno) 
End

1 个答案:

答案 0 :(得分:1)

传入一个字符串作为?分隔列表,然后创建一个UDF以将其拆分。或者,如果您不想要UDF,只需将其拆分为代码即可。