2015-11-02 64 views
1

我想知道如何在存儲過程(EXEC dbo.bisting_sp_common_Event_SetParameter)中通過select語句直接給此變量(@p_txtValue)賦值,而不使用上面的SET語句。如何使用select語句初始化存儲過程中的變量

這是我的代碼,我初始化@l_txtTempSET聲明然後給從它的價值。

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 @[email protected]_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) 

回答

1

唯一的辦法是:

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

和:

因爲
EXEC dbo.bisting_sp_common_Event_SetParameter [email protected]_txtValue = @p_txtValue 

這個例子下面並沒有爲我既不是其他的我想工作

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

這意味着可以直接傳遞字符串'somevalue'或具有可變。

+0

非常感謝你的好解釋:) –

相關問題