我想编写一个存储过程来循环遍历具有成员资格ID和人员ID详细信息的表,并将其作为变量传递给存储过程。
关于如何做的任何想法。任何帮助都非常感谢。
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 :(得分:0)
您有两种选择。第一个是将两个参数传递给过程p_get_details
,每个参数都是comma separated string,包含membership_id和person_id列的连接值。
其他方法是将SELECT查询的整个结果集作为Table Valued Parameter传递给过程。
答案 1 :(得分:0)
如果你需要一次又一次地为不同的表值调用proc,那么你可以创建一个包含所有成员资格和人员的临时表
请参阅此代码是否适合您: -
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
membership_id=@membership_id
person_id =@person_id
end