and if so what happens if I am within a cursor, can I come back to it and
continue?
Thanks.
Denny Cherry - 11 Jul 2008 01:34 GMT
Yes a stored procedure can call another stored procedure, and yes you
can call one from within a cursor. After the child procedure is run,
the calling code will continue as normal.
Denny
>and if so what happens if I am within a cursor, can I come back to it and
>continue?
>Thanks.
Eric Isaacs - 11 Jul 2008 01:41 GMT
Yes you can call a stored procedure from within another sproc, and yes
you can do it within a cursor. What do you mean by can you come back
to it? Do you mean can you call it again and again? If so, yes.
To execute a sproc from another sproc that doesn't return any results,
just use the EXEC keyword before the sproc your calling...
CREATE PROCEDURE Testing123
AS
select 1 as ItWorks
GO
CREATE PROCEDURE Testing1234
AS
EXEC Testing123
GO
EXEC testing1234
GO
If the procedure you're calling returns a result set, you can insert
those results into a temp a table and process those within the other
procedure...
CREATE PROCEDURE Testing12
AS
CREATE TABLE #T
(
TestValue INT
)
INSERT INTO #T
EXEC Testing123
SELECT * FROM #T
GO
EXEC testing12
If you're doing that within a cursor, you'll probably want ot create
the table before the cursor loop and clear it between loops.
If you provide more detailed information about what you need, you will
probably get a better answer.
I hope that helps,
-Eric Isaacs
Erland Sommarskog - 11 Jul 2008 09:37 GMT
> and if so what happens if I am within a cursor, can I come back to it and
> continue?
The cursor continues where it left off, unless you fiddled with the cursor
in the procedure.
A good habit is always make you cursors static:
DECLARE cur CURSOR STATIC LOCAL FOR
The result set for a static cursor is fixed once for all. Other cursor
types can give you nasty surprises.
The LOCAL keyword makes the cursor visible only to the scope it was created
in. The main advantage is that if you procedure is interrupted, the cursor
goes away too.

Signature
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx