Solved

Execute stored procedure inside a stored procedure

Posted on 2008-10-21
3
2,069 Views
Last Modified: 2012-05-05
I have an execute statement for a stored procedure inside of a stored procedure (template below) , the execute is set to run after an inset into a table. The StoredProc2 is dependent on the inserted record. I run the StoredProc1 in a loop from my vb.net code.
For some reason it seems like occasionally the StoredProc2 that depends on the insert in StoredProc1
executes before the insert completes. Is there a way I can force my StoredProc2 run only after the insert completes.

StoredProc1
 Begin
             INSERT INTO  MyTbl &

            EXECUTE StoredProc2 &
 End

Thanks
Vb

0
Comment
Question by:vbothello
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 125 total points
ID: 22771475
>For some reason it seems like occasionally the StoredProc2 that depends on the insert in StoredProc1
executes before the insert completes.

that is not possible. TSQL is executed sequentially.

you must be "observing" incorrectly.
0
 
LVL 31

Assisted Solution

by:James Murrell
James Murrell earned 125 total points
ID: 22771490
i have used this before http://www.aspfree.com/c/a/ASP.NET-Code/Call-Stored-procedure-from-within-another-stored-procedure-return-values/

This quick how-to shows an example of having two stored procedures that one stored procedure executes the other stored procedure passing values and returning a value to the calling parent stored proc. This would be handy passing in a value to dynamically populate a lookup table and stored that Id as a foreign key in a datatable. . This example asssumes you would have a parent table and a child/lookup table related back to the parent table.

'This stored procedure is called by a bu ...This quick how-to shows an example of having two stored procedures that one stored procedure executes the other stored procedure passing values and returning a value to the calling parent stored proc.  This would be handy passing in a value to dynamically populate a lookup table and stored that Id as a foreign key in a datatable.  .  This example asssumes you would have a parent table and a child/lookup table related back to the parent table.
[code]
'This stored procedure is called by a business component or webpage
'passing into two parameters, executes sp_ChildStoredProc and passes on value.
'The @Id OUTPUT returns the value.  

CREATE PROCEDURE sp_ParentStoredProc
(
@col1 varchar(20)
@ValuePassed varchar(50),
)
AS

declare @Id int
EXEC sp_ReturnValue @ValuePassed, @Id OUTPUT

INSERT INTO SomeTable
(col1, col2)
VALUES
(@col1, @Id)
RETURN
GO


'This stored procedure is executed in the sp_ParentStoredProc above and passes'a varchar variable piece of data. The spChildStoredProc will insert this piece of data'into a lookup table if it doesn't exist, return the Identity column using Scope_Identity() function or'will return the identity value to the parent stored proc if the piece of data passed in already exists in the lookup table.

CREATE PROCEDURE sp_ChildStoredProc

(
@ValuePassedIn varchar(50),
@Id smallint OUTPUT
)
 AS

If exists (SELECT column1 FROM LookUpTable WHERE column1=@ValuePassedIn)
BEGIN
SET @Id = (SELECT column1
   FROM LookUpTable
   WHERE column1=@ValuePassedIn)
END
ELSE
BEGIN
INSERT INTO LookUpTable(column1)
VALUES(@ValuePassedIn)
   
    SET @Id = Scope_Identity()
END
GO
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question