sql - Loop through all the rows of a temp table and call a stored procedure for each row -
i have declared temp table hold required values follows:
declare @temp table ( password int, idtran int, kind varchar(16) ) insert @temp select s.password, s.idtran, 'test' signal s inner join vefify v on s.password = v.password , s.idtran = v.idtran , v.type = 'dev' s.[type] = 'start' , not exists (select * signal s2 s.password = s2.password , s.idtran = s2.idtran , s2.[type] = 'progress' ) insert @temp select s.password, s.idtran, 'test' signal s inner join vefify v on s.password = v.password , s.idtran = v.idtran , v.type = 'prod' s.[type] = 'progress' , not exists (select * signal s2 s.password = s2.password , s.idtran = s2.idtran , s2.[type] = 'finish' )
now need loop through rows in @temp table , and each row call sp takes parameters of @temp table input. how can achieve this?
you need use cursor:
declare @id int declare @pass varchar(100) declare cur cursor select id, password @temp open cur fetch next cur @id, @pass while @@fetch_status = 0 begin exec mysp @id, @pass ... -- call sp here fetch next cur @id, @pass end close cur deallocate cur
Comments
Post a Comment