Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 905
wabregoc Avatar asked Jan 28 '15 21:01

wabregoc


People also ask

How does SQL Server handle no data found exception?

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.

What is the SQL code of exception with Name No data found?

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.

When no data found exception in Oracle continue?

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.

What are SQL Server exceptions?

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.

What is Ora 01403 No data found?

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.


2 Answers

DECLARE @rc INT = 1
IF NOT EXISTS(SELECT * FROM table WHERE ...) SET @rc = 0
RETURN @rc
like image 122
ANDREI FOMITCHEV Avatar answered Oct 11 '22 22:10

ANDREI FOMITCHEV


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)

like image 23
Nick Fotopoulos Avatar answered Oct 11 '22 23:10

Nick Fotopoulos