Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Error handling Expand / Collapse
Author
Message
Posted Monday, February 3, 2014 11:25 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, September 19, 2014 12:12 AM
Points: 43, Visits: 42
I have created following table for testing purposes:
CREATE TABLE TESTTABLE 
(
id int not null,
txt1 varchar(20) not null,
int2 int not null
)

Example 1. Insert first record
insert TESTTABLE
select 1, 'Text1', 1
if @@ERROR <> 0
print 'error occured'

Result: The record is succesfully inserted.

Example 2. Insert second record:
insert TESTTABLE
select 2, 'Text2xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 2
if @@ERROR <> 0
print 'error occured'

Result = 'error occured' (msg 8152 - string or binary data would be truncated). @@ERROR catches the error.

Example 3. Insert third record:
insert TESTTABLE
select 3, 'Text3', 'Text3'
if @@ERROR <> 0
print 'error occured'

This insertion results in a syntax error (msg 245 - Conversion failed when converting the varchar value 'Text4' to data type int. @@ERROR DOES NOT catch the error.

4. Example Insert fourth record:
insert TESTTABLE
select 4, 'Text4', 4, 'Text4'
if @@ERROR <> 0
print 'error occured'

This insertion results in a syntax error (msg 213 - column name or number of supplied values does not match table definition. @@ERROR DOES NOT catch the error.

QUESTION:
Does anyone know how to catch errors in example 3 and 4?

Thanks

/Mikkel
Post #1537433
Posted Monday, February 3, 2014 1:52 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 9:10 AM
Points: 13,230, Visits: 12,709
Generally speaking using try/catch is preferred to examining @@error.

Using try/catch will successfully trap the runtime errors encountered for items 1, 2 and 3.

begin try
insert TESTTABLE
select 1, 'Text1', 1
end try
begin catch
print 'error occured'
end catch

begin try
insert TESTTABLE
select 2, 'Text2xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 2
end try
begin catch
print 'error occured'
end catch

begin try
insert TESTTABLE
select 3, 'Text3', 'Text3'
end try
begin catch
print 'error occured'
end catch

However, the error in #4 is going to throw a compile error. That means it will parse fine but the exception is thrown when the engine compiles your statement. That means it can't be caught because it actually happens prior to executing.



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1537500
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse