I need SQL Server equivalent of Oracle’s “when no data found” exception example:
EXCEPTION
WHEN NO_DATA_FOUND
THEN
RETURN 0;
WHEN OTHERS
THEN
RETURN 0;
converter to sql server
1. no_data_found :- Whenever PL/SQL Block having select into clause and also if requested data is not available then oracle server returns an error ora – 1403 : no data found. For handling this error oracle provided no_data_found exception name.
For internal exceptions, SQLCODE returns the number of the Oracle error. The number that SQLCODE returns is negative unless the Oracle error is no data found, in which case SQLCODE returns +100.
By putting your select statement inside it's own execution block with it's own exception handler, you can choose to ignore no_data_found exceptions and thus allow the loop to continue.
An error condition during a program execution is called an exception and the mechanism for resolving such an exception is known as exception handling. In this article, we will learn how to implement exception handling in SQL Server. SQL Server provides TRY, CATCH blocks for exception handling.
The ORA-01403 error derives from an SQL query meant to return all data requested, but no data was found. The error is often associated with SELECT INTO clauses, which fetch rows or sets of columns from a database. The data gathered is then inserted into a set of predefined variables.
DECLARE @rc INT = 1
IF NOT EXISTS(SELECT * FROM table WHERE ...) SET @rc = 0
RETURN @rc
If you really need to use exception handling then you can do the following:
BEGIN TRY
/* your other code */
IF NOT EXISTS(SELECT * FROM table WHERE [...])
THROW 50000, 'Your error messages here!', 1
/* more code */
END TRY
BEGIN CATCH
/* exception handling goes here */
END CATCH
If not, the you can just use IF NOT EXISTS (SELECT * FROM blah)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With