Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / Programming / SQL / July 2008

Tip: Looking for answers? Try searching our database.

Can a stored proc call another stored proc?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
mscertified - 11 Jul 2008 01:26 GMT
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

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.