2014-04-29 47 views
0

我目前有一個查詢,它根據先前查詢的結果在臨時表中運行n個插入。放入臨時表中的結果來自一個sproc。使用臨時表中的sproc連接變量插入

我也想在表中插入一個正在使用的變量(但在sproc中沒有使用)。例如是這樣的:

--somewhere up top 
Declare @userID int --This is used to get info before the important part 
Declare @someOtherID int --This is passed into the sproc 

--Some stuff happens here that is unrelated to question 

--Important part 
while (@@rowcount > 0) 
begin 
    insert @tmp2 exec spThisSprocDoesntContainOrCareAboutUserID @someOtherID 
    delete from @tmp where [someID][email protected] 
    select top 1 @id=[id], @someOtherID=[someID] from @tmp 
end 

SELECT * FROM @tmp2 

我想要做的也插入在@每行TMP2在上一節中使用的用戶ID。類似於

insert @tmp2 @userID + exec spThisSprocDoesntContainOrCareAboutUserID @someOtherID 

很明顯,這是行不通的。但是,這是我想要做的一般想法。

有沒有人對此有任何建議?

(注意,我不希望修改sproc /做一個類似的變量,只是爲了將結果傳回給結果,如果我可以幫助它的話,也不是不可能的,只是不希望)

+0

爲什麼這個封裝在一個存儲過程?如果它被封裝在一個用戶定義的函數中,那麼你有更多的靈活性(加入結果,修改它們,過濾它們等)。存儲過程仍然可以調用udf以實現向後兼容性等。 – MatBailie

回答

1

您可以通過一個額外的步驟去做(因爲你沒有SP任何修改提及)

insert @tmp3 exec spThisSprocDoesntContainOrCareAboutUserID @someOtherID 
insert @tmp2 Select @userId, field1, field2 from @temp3