Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL exexute store procedure with results of a query.

Posted on 2016-08-29
3
Medium Priority
?
79 Views
Last Modified: 2016-08-31
Hi,

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:

id
16777359
16777357
16777361
16777358
16777360

Thanks,

ward.


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

0
Comment
Question by:whorsfall
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 53

Assisted Solution

by:Ryan Chong
Ryan Chong earned 1000 total points
ID: 41775761
you need to open a cursor.

for examples:

check the syntax at the section: Why Not Just Use a Cursor?
https://www.mssqltips.com/sqlservertip/1414/run-same-command-on-all-sql-server-databases-without-cursors/

Using Cursor in SQL Server Stored Procedure with example
http://www.aspsnippets.com/Articles/Using-Cursor-in-SQL-Server-Stored-Procedure-with-example.aspx
0
 
LVL 52

Expert Comment

by:Vitor Montalvão
ID: 41776504
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.
0
 
LVL 9

Accepted Solution

by:
Valliappan AN earned 1000 total points
ID: 41776630
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
BEGIN   
   SELECT @id = id FROM #temp WHERE serial = @serial 
   
   EXEC spUpdateAppDeploymentSummaryByCollection @AssignmentID = @id
   
   SET @serial = @serial + 1
END

DROP TABLE #temp
Go

Open in new window

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question