SQL Server equivalent of Oracle’s “when no data found” exception

120 pts.
Tags:
SELECT statement
Stored Procedure variables
Stored Procedures
In a stored procedure, I'm storing the result of a select statement in @variable. Is there a way to capture a SQL Code 100, like Oracle's SQLERRM(100)/ORA-01403 if no records are returned? I'm currently setting @variable = '*' before the select and and IF @variable = '*' afterwards, but there must be a more elegant way - isn't there?
ASKED: August 15, 2008  2:41 PM
UPDATED: March 6, 2012  5:44 PM

Answer Wiki

Thanks. We'll let you know when a new response is added.

After the select is executed check the value of the @@ROWCOUNT system variable. If it is = 0 then there were no records found. You can then use the RAISERROR system function to through a message saying this which would be returned to the client.

Discuss This Question: 2  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • Denny Cherry
    Check out my SQL Server blog "SQL Server with Mr Denny" for more SQL Server information.
    65,490 pointsBadges:
    report
  • Faizkazi4u
    if no records are there it will return one row and with null value.
    10 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following