Multiple Insert statement in a stored procedure

Hello Experts,

I'd like to create a stored procedure where it involves multiple inserts depending on some conditions. But unfortunately this could not be compiled or incorrect syntax. Any thoughts on where i need to change?

CREATE PROCEDURE SP1
@MyID int
AS
IF
(SELECT Count(*) FROM Tbl1 WHERE MyID=@MyID) = 0
BEGIN
INSERT INTO MyTable ---This table has an Identity column which is required on the next insert!!!
(Col1,
Col2,
MyID)
(Select Col1, Col2, MyID FROM Tbl2 WHERE MyID=@MyID)

INSERT INTO FinalTable
(Id---Identity value created by previous insert statement above!!!
,Col31
,Col32
,Col33)
(SELECT b.Id, a.Col31, a.Col32, a.Col33 FROM Tbl3 a INNER JOIN MyTable b ON a.MyID=b.MyID

ELSE
SELECT COUNT(*) FROM FinalTable

END
jsuanqueAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kalpesh ChhatralaSoftware ConsultantCommented:
1) you did not provided error description. without error description expert can not help you.

2) try below link for multiple insert statement in stored procedure.

http://stackoverflow.com/questions/14521416/more-than-one-insert-statements-in-stored-procedure-one-give-error
0
Louis01Commented:
Something like this:
CREATE PROCEDURE SP1
@MyID int
AS
IF (SELECT Count(*) FROM Tbl1 WHERE MyID=@MyID) = 0
BEGIN 
    DECLARE @MyTableVar table( InsertedID int, MyID int);

    INSERT INTO MyTable (Col1,Col2,MyID)
    OUTPUT INSERTED.ID, INSERTED.MyID INTO @MyTableVar    
    SELECT Col1, Col2, MyID 
      FROM Tbl2 
     WHERE MyID=@MyID;

    INSERT INTO FinalTable (@InsertedID,Col31,Col32,Col33)
    SELECT b.InsertedID, a.Col31, a.Col32, a.Col33 
      FROM Tbl3 a 
            INNER JOIN @MyTableVar b 
        ON a.MyID=b.MyID
END

Open in new window

http://technet.microsoft.com/en-us/library/ms177564.aspx
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jsuanqueAuthor Commented:
Hello Louis01,
Your suggestion works like charm Thanks.

Hello kalpesh2804,
Thanks for the link, that would be helpful on my succeeding tasks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.