Avatar of Larry Brister
Larry Brister
Flag for United States of America asked on

SQL Server error on select

Is there a way to  handle a error on my select and write to a table if so?

For instance

If  Select 4/0  errors (and it will)
Insert into myTable (col1)
values ('Error on select statement')

Or if I have an error on a HUGE select statement do the same thing.
Microsoft SQL Server 2005Microsoft SQL ServerMicrosoft Development

Avatar of undefined
Last Comment
Jim Horn

8/22/2022 - Mon
deighton

BEGIN TRY
  Select 4/0  
END TRY
BEGIN CATCH
print 'yeah it went wrong'
END CATCH
tim_cs

ThomasMcA2

IF EXISTS (SELECT * FROM TableName) 
  /* select works */
 ELSE 
  /* select failed */

Open in new window

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Larry Brister

ASKER
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.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Larry Brister

ASKER
guys...
There are supposed to be two result sets?

No way to just return the select OR the error...not both?
ASKER CERTIFIED SOLUTION
Jim Horn

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
keyu

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.
 

>>-RAISE_ERROR--(--sqlstate--,--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
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Larry Brister

ASKER
THanks
Jim Horn

Thanks for the grade.  Good luck with your project.  -Jim