2014-03-28 50 views
-1

我想編寫一個存儲過程來遍歷具有成員資格id和個人id詳細信息的表並將其作爲變量傳遞給存儲過程。while循環獲取存儲過程的兩個變量

有關如何去做的任何想法。任何幫助,高度讚賞。

DECLARE @membership NUMERIC(9) 
DECLARE @person_id NUMERIC(9) 
DECLARE @id_num INT 
WHILE (
set @id_num=id_num+1 
SELECT membership_id,person_id FROM [dbo].[reb] WHERE id_num <1160 


EXEC p_get_details @membership_id, person_id 

回答

0

您有兩種選擇。第一個是將兩個參數傳遞給程序p_get_details,每個參數都是comma separated string,其中包含membership_id和person_id列的連接值。

其他是將SELECT查詢的整個結果集作爲Table Valued Parameter傳遞給過程。

+0

你能告訴我用上述說明怎麼做嗎? – user3120927

0

如果您需要一遍又一遍撥打PROC不同的表值然後ü可以創建擁有所有的會員和personids

看到一個臨時表如果此代碼對你的作品: -

DECLARE @membership_id NUMERIC(9) 
DECLARE @person_id NUMERIC(9) 


if(OBJECT_ID('tempdb..#tempids') is not null)  
drop table #tempids  

SELECT membership_id,person_id 
into #tempids 
FROM [dbo].[reb] 
WHERE id_num <1160 

while exists(select 1 from #tempids) 
begin 
-- select 1 row which will be called by proc 

select top 1 @membership_id=membership_id 
@person_id =person_id 
from #tempids 

EXEC p_get_details @membership_id, @person_id 

--delete the ids which have been called by proc 
delete from #tempids 
where 
[email protected]_id 
person_id [email protected]_id 


end