Link to home
Start Free TrialLog in
Avatar of CSecurity
CSecurityFlag for Iran, Islamic Republic of

asked on

TRY CATCH in SQL QUERY

I have a MS SQL 2005 server, I have a FOR WHILE

Statement and it works properly on my database and do some stuff on all tables.

I got a problem, for some unknown reason sometimes it get a problem on 1 field, on 1 table,

I want it to bypass and continue execution and not stop at error...

Something like try catch or ON ERROR RESUME NEXT

Is it a case in SQL?

Please advice

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Aneesh
Aneesh
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Within your WHILE clause try using the below approach:
while (a <100)
-- your operation
select * from table

-- capture error code
if @@error <> 0
--  log into some tables

a = a + 1;

By this approach, you will be able to run your statements without exiting out of the other processes.
I suppose you mean T-SQL. and then, yes you can. See the code snippet for the syntax.
You can also check http://blog.sqlauthority.com/2007/04/11/sql-server-2005-explanation-of-trycatch-and-error-handling/ or http://msdn.microsoft.com/en-us/library/ms179296.aspx

BEGIN TRY
{ sql_statement |
statement_block }
END TRY
BEGIN CATCH
{ sql_statement |
statement_block }
END CATCH

Open in new window