1

I am using SQL Server 2012.

How to continue Query Execution if any error occurs? If in the following example 2nd query fails then rest queries didn't execute.

INSERT Schema1.[Menu] ([CompanyId], [Menu], [Role]) VALUES (5, N'Transaction', 2)
INSERT Schema2.[Menu] ([CompanyId], [Menu], [Role]) VALUES (5, N'Transaction', 2)
INSERT Schema3.[Menu] ([CompanyId], [Menu], [Role]) VALUES (5, N'Transaction', 2)
INSERT Schema4.[Menu] ([CompanyId], [Menu], [Role]) VALUES (5, N'Transaction', 2)

How to solve this?

1
  • put every insert into a try catch ? Commented Oct 7, 2016 at 7:18

4 Answers 4

1

Or if you don't want to log anything you can just separate the queries by using GO

INSERT Schema1.[Menu] ([CompanyId], [Menu], [Role]) VALUES (5, N'Transaction', 2)
GO
INSERT Schema2.[Menu] ([CompanyId], [Menu], [Role]) VALUES (5, N'Transaction', 2)
GO
INSERT Schema3.[Menu] ([CompanyId], [Menu], [Role]) VALUES (5, N'Transaction', 2)
GO
INSERT Schema4.[Menu] ([CompanyId], [Menu], [Role]) VALUES (5, N'Transaction', 2)
GO
Sign up to request clarification or add additional context in comments.

3 Comments

Exactly. I don't want to Log anything. I will try this.
Resulting in 3 unhandled errors. Even if you don't want to log anything, try..catch will catch the errors so, albeit loosely, they are handled.
He/she just wanted to continue the query, nothing else.
0

You could wrap each statement in a try..catch block.

That way you could log failures and no exactly waht didn't execute as expected.

See link below for try catch syntax.

https://msdn.microsoft.com/en-us/library/ms175976.aspx

Comments

0

try this:

begin try 
    INSERT Schema1.[Menu] ([CompanyId], [Menu], [Role]) VALUES (5, N'Transaction', 2)
end try
begin catch
     -- what you want to do in catch
end catch    

begin try 
    INSERT Schema2.[Menu] ([CompanyId], [Menu], [Role]) VALUES (5, N'Transaction', 2)
end try
begin catch
      -- what you want to do in catch
end catch    

and so on...

2 Comments

If i remove the catch statements & keep only try, Will it work?
no. If you start a try you have to and it with catch. The catch may be empty offcourse
0
   -- maybe like this :)
    -- using tran catch

    BEGIN TRY
    BEGIN TRAN
     INSERT Schema1.[Menu] ([CompanyId], [Menu], [Role]) 
     VALUES (5,   N'Transaction', 2)
     INSERT Schema2.[Menu] ([CompanyId], [Menu], [Role])
    VALUES (5, N'Transaction', 2)
    INSERT Schema3.[Menu] ([CompanyId], [Menu], [Role]) 
    VALUES (5, N'Transaction', 2)
   INSERT Schema4.[Menu] ([CompanyId], [Menu], [Role]) 
   VALUES (5, N'Transaction', 2)
   COMMIT TRAN
  END TRY
   BEGIN CATCH
   ROLLBACK TRAN
  END CATCH

3 Comments

this will undo the succeeded inserts if just one fails. I dont see the differences with the OP original code
your originals code if fails one, others can execute if not fails. and my code if only one fails or more, all is fails too...
your code will result in all inserts rollbacked if one or more fails. That is exact what the OP does NOT wants

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.