Click here to Skip to main content
15,911,848 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,

I have the below 2 queries in a stored procedure:

SQL
insert into Names(PhysicianId,FName,LName) values(@PhysicianId,@FName,@LName)

insert into dummy(DummyName,DummyCity) values('xxx','yyy')


Here, the second query should get executed if and only if the first query executes.
i.e., when the code reaches the stored procedure with the respective parameters and if there is any issue with the first query, the second query also should not get executed.


Is there any way for doing this?

I would greatly appreciate your suggestion on this.


Thanks
Posted
Updated 7-Nov-12 9:38am
v2

1 solution

You can use Tansaction


Like this


SQL
BEGIN TRANSACTION;

BEGIN TRY
   insert into Names(PhysicianId,FName,LName) values(@PhysicianId,@FName,@LName)
 
   insert into dummy(DummyName,DummyCity) values('xxx','yyy')END TRY
BEGIN CATCH
    SELECT
        ERROR_NUMBER() AS ErrorNumber
        ,ERROR_SEVERITY() AS ErrorSeverity
        ,ERROR_STATE() AS ErrorState
        ,ERROR_PROCEDURE() AS ErrorProcedure
        ,ERROR_LINE() AS ErrorLine
        ,ERROR_MESSAGE() AS ErrorMessage;

    IF @@TRANCOUNT > 0
        ROLLBACK TRANSACTION;
END CATCH;

IF @@TRANCOUNT > 0
    COMMIT TRANSACTION;
GO
 
Share this answer
 
Comments
Raj.rcr 7-Nov-12 16:19pm    
This is what I was exactly looking for... Thanx soooo much :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900