SQL Server 'Resume Next' Equivalent

25,548

Solution 1

Actually your batch executed until the end since key violations are not intrerupting batch execution. If you run the same SQL file from Management Studio you'll see that the result is that all the valid statements were executed and the messages panel contains an error for each key violation. The SqlClient of ADO.NEt behaves much the same way, but at the end of the batch (when SqlCommand.ExecuteNonQuery returns) it parses the messages returned and throws an exception. The exception is one single SqlException but it's Errors collection contains a SqlError for each key violation that occured.

Unfortunately there is no silver bullet. Ideally the SQL files should not cause errors. You can choose to iterate through the SqlErrors of the exception and decide, on individual basis, if the error was serious or you can ignore it, knowing that the SQL files have data quality problems. Some errors may be serious and cannot be ignored. See Database Engine Error Severities.

Another alternative is to explictily tell the SqlClient not to throw. If you set the FireInfoMessageEventOnUserErrors property of the connection to true it will raise an SqlConnection.InfoMessage event instead of twroing an exception.

Solution 2

SQL Server does have a Try/Catch syntax. See:

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

To use this with your file, you would either have to rewrite the files themselves to wrap each line with the try/catch syntax, or else your code would have to programatically modify the file contents to wrap each line.

There is no T-SQL equivalent of "On Error Resume Next", and thank Cthulhu for that.

Solution 3

At the risk of slowing down the process (by making thousands of trips to SQL server rwther than one), you could handle this issue by splitting the file into multiple individual queries each for either INSERT or UPDATE. Then you can catch each individual error as it take place and log it or deal with it as your business logic would require.

Solution 4

begin try
 --your critical commands
end try
begin catch
  -- is necessary write somethink like this
  select ''
end cath
Share:
25,548
Irakli Kobalava
Author by

Irakli Kobalava

I am a Development Team Lead with more than 20 years of development experience, mostly in the financial sector.

Updated on January 10, 2020

Comments

  • Irakli Kobalava
    Irakli Kobalava over 4 years

    I'm working on a project in VB.net which takes large text files containing T-SQL and executes them against a local SQL database, but I've hit a problem in regards to error handling.

    I'm using the following technologies :

    • VB.net
    • Framework 3.5
    • SQL Express 2005

    The SQL I'm trying to execute is mostly straight-forwards but my app is completely unaware of the schema or the data contained within. For example :

    UPDATE mytable SET mycol2='data' WHERE mycol1=1
    INSERT INTO mytable (mycol1, mycol2) VALUES (1,'data')
    UPDATE mytable SET mycol2='data' WHERE mycol1=2
    INSERT INTO mytable (mycol1, mycol2) VALUES (1,'data')
    UPDATE mytable SET mycol2='data' WHERE mycol1=3
    

    The above is a sample of the sort of thing I'm executing, but these files will contain around 10,000 to 20,000 statements each.

    My problem is that when using sqlCommand.ExecuteNonQuery(), I get an exception raised because the second INSERT statement will hit the Primary Key constraint on the table.

    I need to know that this error happened and log it, but also process any subsequent statements. I've tried wrapping these statements in TRY/CATCH blocks but I can't work out a way to handle the error then continue to process the other statements.

    The Query Analyser seems to behave in this way, but not when using sqlCommand.ExecuteNonQuery().

    So is there a T-SQL equivalent of 'Resume Next' or some other way I can do this without introducing massive amounts of string handling on my part?

    Any help greatly appreciated.