Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
asn187
Starting Member
9 Posts |
Posted - 2007-04-25 : 08:37:51
|
I am running the query below to populate a temporary table with some data as below.CREATE TABLE #memberSchoolRows (memberID uniqueidentifier, orgID int)INSERT INTO #memberSchoolRows (memberID, orgID) SELECT [dbo].org_member.member_id, [dbo].org.org_id FROM [dbo].org INNER JOIN [dbo].org_member ON [dbo].org_member.org_id = [dbo].org.org_id WHERE [dbo].org.org_id = 'A49409DB-2142-46B7-AD77-54D1B56BCB06' I then want to execute a stored procedure for each row in the temporary table using the data in the two columns of the temp tableexec sp_Populate @memberID, @orgIDHowever, how would i loop through the temporary table and then executing the stored proc for each row in the temp table? |
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-04-25 : 08:53:03
|
It would be better if you could re-write your SP so that it can work on entire temp table at once rather than looping through each record in temp table.Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
asn187
Starting Member
9 Posts |
Posted - 2007-04-25 : 08:59:56
|
this wont be possible as it deals with many updates and inserts etc |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-04-25 : 09:04:22
|
quote: Originally posted by asn187 this wont be possible as it deals with many updates and inserts etc
Impossible or just need more work ?  KH |
 |
|
asn187
Starting Member
9 Posts |
Posted - 2007-04-25 : 09:17:15
|
think i may have solved it...think the below should work?CREATE TABLE #memberSchoolRows (memberID uniqueidentifier, orgID int)INSERT INTO #memberSchoolRows (memberID, orgID)SELECT [dbo].org_member.member_id, [dbo].org.org_idFROM [dbo].orgINNER JOIN [dbo].org_memberON [dbo].org_member.org_id = [dbo].org.org_idWHERE [dbo].org.org_id = 'A49409DB-2142-46B7-AD77-54D1B56BCB06' -- declare a cursor to loop through the temp table-- Declare the variables to store the values returned by FETCH.DECLARE @memberID uniqueidentifier, @orgID intDECLARE schools_cursor CURSOR FORSELECT * FROM #memberSchoolRowsOPEN schools_cursor-- do first fetch and store the values in vars.print 'opened cursor'FETCH NEXT FROM schools_cursorINTO @memberID, @orgID-- check @@FETCH_STATUS see more rows to fetch.WHILE @@FETCH_STATUS = 0BEGIN -- run the stored procedure here to populate the group with these memebr -- get next FETCH NEXT FROM schools_cursor INTO @memberID, @orgIDENDCLOSE schools_cursorDEALLOCATE schools_cursorGO |
 |
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-04-25 : 09:25:07
|
Yikes!! That's cursor there...not the best possible solution.If you can't modify your SP, then add an identity column to the temp table and use a WHILE loop to loop through each record and call SP.Declare @i int, @max intSelect @i = 1, @max = max(id) from #memberSchoolRows While @i <= @maxbegin Select @memberID = memberID, @orgID = OrgID From #memberSchoolRows where ID = @i Exec sp_Populate @memberID, @orgID Set @i = @i + 1end Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
|
|
|
|