Hello -
I'm new to "looping" in MS SQL Server 2K and I have this question:
How can I loop through a database table, for each record in the table I need to execute a stored procedure and update a temporary table based on the SP results? I know how to do everything but the loop..
Here's an example:
Customer table has 100 records. Customer_Id is the PK.
I have a stored procedure that returns an integer when I give it a customer_id.
I can do: exec sp_MyCount 23
The SP will return an the customer ID & an integer (the value is 4, in this case for customer # 23)
So the results of exec sp_MyCount 23 would be
Customer_Id | SPValue
---------------------
23 | 4
It returns one row, two columns (it is the result of a SELECT from a TEMP table inside my SP).
I would like to run this stored procedure for each record in my customers table and insert data into a temp table.
Ideally, the end result (temporary table) would look like this:
SELECT Customer_Id, SPValue FROM #MyTemp
Customer_Id | SPValue
---------------------
1 | 0
2 | 5
3 | 3
4 | 2
5 | 10
Up to the last customer ID in my table.
I cannot for the life of me figure out how to do this simple task?!?
Also - How can I insert the value returned by the SP into my TEMP table.
So - Here's the summary: 2 Questions:
Question # 1:
If I execute an SP, how can I insert the values retuned from that into a temp. table and return the results?
Question # 2:
How can I loop the above operation FOR EACH record in my table?
I hope I'm being clear.. I thought the hard part was writing my SP that returns the INT based on some very funky stuff.. That wasn't too bad -
Looping through the Customers table and exec'ing the SP ON EACH RECORD is what I cannot figure out + I do not know how to insert the results of my SP into a TEMP table.
If someone could answer any one of my 2 questions that would be great. Both would be SUPERB!!
Thanks again!
I'm new to "looping" in MS SQL Server 2K and I have this question:
How can I loop through a database table, for each record in the table I need to execute a stored procedure and update a temporary table based on the SP results? I know how to do everything but the loop..
Here's an example:
Customer table has 100 records. Customer_Id is the PK.
I have a stored procedure that returns an integer when I give it a customer_id.
I can do: exec sp_MyCount 23
The SP will return an the customer ID & an integer (the value is 4, in this case for customer # 23)
So the results of exec sp_MyCount 23 would be
Customer_Id | SPValue
---------------------
23 | 4
It returns one row, two columns (it is the result of a SELECT from a TEMP table inside my SP).
I would like to run this stored procedure for each record in my customers table and insert data into a temp table.
Ideally, the end result (temporary table) would look like this:
SELECT Customer_Id, SPValue FROM #MyTemp
Customer_Id | SPValue
---------------------
1 | 0
2 | 5
3 | 3
4 | 2
5 | 10
Up to the last customer ID in my table.
I cannot for the life of me figure out how to do this simple task?!?
Also - How can I insert the value returned by the SP into my TEMP table.
So - Here's the summary: 2 Questions:
Question # 1:
If I execute an SP, how can I insert the values retuned from that into a temp. table and return the results?
Question # 2:
How can I loop the above operation FOR EACH record in my table?
I hope I'm being clear.. I thought the hard part was writing my SP that returns the INT based on some very funky stuff.. That wasn't too bad -
Looping through the Customers table and exec'ing the SP ON EACH RECORD is what I cannot figure out + I do not know how to insert the results of my SP into a TEMP table.
If someone could answer any one of my 2 questions that would be great. Both would be SUPERB!!
Thanks again!