如何使用select语句初始化存储过程中的变量

时间:2015-11-02 10:56:38

标签: sql-server variables stored-procedures

我想知道如何在存储过程(@p_txtValue)中使用select语句直接为此变量(EXEC dbo.bisting_sp_common_Event_SetParameter)提供一个值,而不使用上面的SET语句。

这是我的代码,我使用@l_txtTemp语句初始化SET,然后从@p_txtValue给出SELECT ssh.No_ ,ssh.[Bill-to Customer No_] ,RANK () over (ORDER BY ssh.No_) Rank_Id INTO #shipments --DROP TABLE #shipments FROM dbo.[Production$Sales Shipment Header] SSH WHERE SSH.[Sell-to Customer No_] IN ('3004', '3003', '3002') AND ssh.[Posting Date]>'2015-10-01' AND NOT EXISTS (SELECT * FROM dbo.[bisting System Monitor] tSub1 WHERE tSub1.[Row Type] = 'Log' AND tSub1.[Type] ='Checkpoint Init' AND tSub1.[Master Event Type] = @l_txtCurrMasterEventType AND tSub1.[Document No_] = ssh.No_) SELECT @l_intRankIDCurr = 1, @l_intRankIDMax = (SELECT MAX(Rank_Id) FROM #shipments) WHILE (@l_intRankIDCurr <= @l_intRankIDMax) BEGIN SELECT @l_txtShipmentNo = No_ FROM #shipments WHERE Rank_Id = @l_intRankIDCurr SET @l_txtTemp = CASE (SELECT t1.[Bill-to Customer No_] FROM #shipments t1 WHERE t1.Rank_Id = @l_intRankIDCurr) WHEN '3002' THEN 'Uppsala.csv' WHEN '3003' THEN 'Gränby.csv' WHEN '3004' THEN 'Örebro.csv' END IF @p_intLogLevel >= 4 PRINT CONVERT(VARCHAR(19),CURRENT_TIMESTAMP,121)+': Create set document no parameter' EXEC dbo.bisting_sp_common_Event_SetParameter @p_bigintEventID=@l_bigintChildEventID ,@p_txtAttributeLvl1='Action SP Filename' ,@p_txtValue= @l_txtTemp SELECT @l_intRankIDCurr += 1 END 的值。

IF @p_intLogLevel >= 4 
   PRINT CONVERT(VARCHAR(19),CURRENT_TIMESTAMP,121)+': Create set document no parameter'

EXEC dbo.bisting_sp_common_Event_SetParameter 
        @p_bigintEventID = @l_bigintChildEventID
        ,@p_txtAttributeLvl1 = 'Action SP Filename'
        ,@p_txtValue = (SELECT [Attribute] 
                        FROM [TABLE] t1 
                        WHERE t1.Rank_Id = @l_intRankIDCurr)

有可能制作这样的东西吗?下面的这个例子对我来说既不适用于我尝试过的其他例子:

STR_TO_DATE

1 个答案:

答案 0 :(得分:1)

唯一的方法是:

declare @p_txtValue nvarchar(max) = 
 (SELECT [Attribute] FROM [TABLE] t1 WHERE t1.Rank_Id = @l_intRankIDCurr)

EXEC dbo.bisting_sp_common_Event_SetParameter ....@p_txtValue = @p_txtValue

因为:

  

[[@parameter =] {value                              | @variable [OUTPUT]                              | [默认]                              }

这意味着您可以直接传递字符串'somevalue'或变量。