views:

8011

answers:

7

Hi,

I would like to execute a stored procedure within a stored procedure eg

EXEC SP1

BEGIN

EXEC SP2 END

But I only want SP1 to finish after SP2 has finished running so I need to find a way for SP1 to wait for SP2 to finish before SP1 ends.

Thanks

+2  A: 

Thats how it works stored procedures run in order, you don't need begin just something like

exec dbo.sp1 exec dbo.sp2

PeteT
You could also wrap it in a transaction just too make sure it all executes correctly.
mattruma
A: 

Sorry my question was not clear.

SP2 is being executed as part of SP1 so I have something like:

CREATE PROCEDURE SP1 AS BEGIN

EXECUTE SP2

END

test
+2  A: 

Here is an example of one of our stored procedures that executes multiple stored procedures within it:

ALTER PROCEDURE [dbo].[AssetLibrary_AssetDelete]
(
    @AssetID AS uniqueidentifier
)
AS

SET NOCOUNT ON

SET TRANSACTION ISOLATION LEVEL READ COMMITTED

EXEC AssetLibrary_AssetDeleteAttributes @AssetID
EXEC AssetLibrary_AssetDeleteComponents @AssetID
EXEC AssetLibrary_AssetDeleteAgreements @AssetID
EXEC AssetLibrary_AssetDeleteMaintenance @AssetID

DELETE FROM
    AssetLibrary_Asset
WHERE
    AssetLibrary_Asset.AssetID = @AssetID

RETURN (@@ERROR)
mattruma
+4  A: 

T-SQL is not asynchronous, so you really have no choice but to wait until SP2 ends. Luckily, that's what you want.

CREATE PROCEDURE SP1 AS
   EXEC SP2
   PRINT 'Done'
Mark Brackett
A: 

Hi I have found my problem is that SP2 doesn't execute from within SP1 when SP1 is executed.

Below is the structure of SP1:

ALTER PROCEDURE SP1 AS BEGIN

Declare c1 cursor....

open c1 fetch next from c1 ...

while @@fetch_status = 0 Begin

...

Fetch Next from c1 end

close c1

deallocate c1

exec sp2

end

test
Next time just post the entire code ... it will save us all a lot of time and effort.
mattruma
A: 

Your SP2 is probably not running because of some failure in the early code in SP1 so EXEC SP2 is not reached.

Please post your entire code.

Cade Roux
A: 

Looks like this question is a duplicate. Please only ask your question once.

Rick