Something strange here
Running the Select statement below only returns a error
Howerev...running that all together returns TWO record sets? I only want the one dataset.
Either the results...or the error
BEGIN TRY
SELECT CAST('<root><MissingNote>6/12 < 6/23</MissingNote></root>' AS XML)
END TRY
BEGIN CATCH
SELECT 1
END CATCH ;
Jim Horn
To build on the link that tim_cs provided and everyone's TRY..CATCH comment
BEGIN TRY
SELECT 4/0
SELECT 'if this displays then error handling didnt work'
END TRY
BEGIN CATCH
Insert into myTable (no, severity, state, procedure, line, message)
SELECT ERROR_NUMBER(),ERROR_SEVERITY(),ERROR_STATE(),ERROR_PROCEDURE(),ERROR_LINE(),ERROR_MESSAGE()
END CATCH
deighton
what are you running the SQL command in?
if I run just
SELECT CAST('<root><MissingNote>6/12 < 6/23</MissingNote></root>' AS XML)
it in a management studio window
I get an error and an empty result
if I run it with try catch, I get the empty results, no error message and whatever my catch does.
The RAISE_ERROR function causes the statement that invokes the function to return an error with the specified SQLSTATE (along with SQLCODE -438) and diagnostic string.
SELECT EMPNO,
CASE WHEN EDLEVEL < 16 THEN 'Diploma'
WHEN EDLEVEL < 18 THEN ’Graduate’
WHEN EDLEVEL < 21 THEN ’Post Graduate’
ELSE RAISE_ERROR( '07001',
'EDLEVEL has a value greater than 20' )
END
FROM EMPLOYEE
or as mentioned jimhorn you can go for this....
BEGIN TRY
SELECT 4/0
END TRY
BEGIN CATCH
Insert into ERROR_TABLE(err_num,err_sevr,err_sta,err_state,err_proc,err_line,err_msg) values(ERROR_NUMBER(),ERROR_SEVERITY(),ERROR_STATE(),ERROR_PROCEDURE(),ERROR_LINE(),ERROR_MESSAGE())
SELECT * FROM ERROR_TABLE
END CATCH
Larry Brister
ASKER
It's looking like jimhorns answer is best and just handle it in other ways.
Will award points shortly without anyones strenous objections
Select 4/0
END TRY
BEGIN CATCH
print 'yeah it went wrong'
END CATCH