Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL Server Stored Procedure

Status
Not open for further replies.

olibh

Programmer
Mar 21, 2002
13
0
0
IE
I want to create a stored procedure that will select records meeting the required criteria, then loop though and run another stored procedure on each record individually before proceeding to the next - is this possible ?

Any help would be greatly appreciated

e.g.

CREATE PROCEDURE sp_test

as

DECLARE @sql nvarchar(4000)

SET @sql = "select * from dbo.communications where Received = 'Initiated'"

While Not @sql.EOF

exec dbo.sp_genxml
Rs.MoveNext


GO
 
You need to loop at sql loops or cursors...see Books On Line

Andy
 
You need a cursor something like this:

Code:
DECLARE @col varchar(50)
DECLARE exec_cur CURSOR FOR
  SELECT col1 FROM tablename WHERE col2 = 'xxx'

OPEN exec_cur

FETCH NEXT FROM exec_cur INTO @col

WHILE @@FETCH_STATUS <> -1
BEGIN
  EXEC spname @col
  FETCH NEXT FROM exec_cur INTO @col
END

You just need to declare your variables with the right data type, plus you can add more if you need to send more parameters to the SP.
 
I want to out the contents of a field in a record to a file and then update the contents of another field called STATUS to &quot;Exported&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top