SQL exexute store procedure with results of a query.


How can I execute a store procedure with the results of a sql query. Here is my attempt that I only got half way with.

In the example below I want to take the AssigmentID and execute each value returned to call a stored procedure.

In this example the ids that would return from the first select are:




declare @test table (id int);

insert into @test select AssignmentID from CI_CIAssignments

select * from @test

exec spUpdateAppDeploymentSummaryByCollection @AssignmentID = 16777359
exec spUpdateAppDeploymentSummaryByCollection @AssignmentID = 16777357
exec spUpdateAppDeploymentSummaryByCollection @AssignmentID = 16777361
exec spUpdateAppDeploymentSummaryByCollection @AssignmentID = 16777358
exec spUpdateAppDeploymentSummaryByCollection @AssignmentID = 16777360

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongSoftware Team Lead, ex-Business Systems Analyst , ex-Senior Application EngineerCommented:
you need to open a cursor.

for examples:

check the syntax at the section: Why Not Just Use a Cursor?

Using Cursor in SQL Server Stored Procedure with example
Vitor MontalvãoMSSQL Senior EngineerCommented:
Can you change the spUpdateAppDeploymentSummaryByCollection to receive a table instead of a value? That way you can send all values one time only so it will perform it all in one single step.

You can also post here the code of that Stored Procedure so we can find an alternative way to do this.
Valliappan ANSenior Tech ConsultantCommented:
you could do something like this:

SELECT Identity(INT, 1,1) AS serial, AssignmentID INTO #temp FROM CI_CIAssignments

DECLARE @maxserial INT, @serial INT, @Id INT

SELECT @maxserial=MAX(serial) FROM #test     --start with the highest primary key serial

SET @serial = 1

WHILE @serial > @maxserial                          --loop until no more records
   SELECT @id = id FROM #temp WHERE serial = @serial 
   EXEC spUpdateAppDeploymentSummaryByCollection @AssignmentID = @id
   SET @serial = @serial + 1


Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.