Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I am unable to use THROW SQL Server 2008 R2

Tags:

SQL Server 2008 R2 Management Studio does not recognized my throw in the below example, it says

incorrect syntax near Throw

I am trying to throw an error here, so I can handled it in my website when someone insert the same value twice.

Begin Try  insert into BusinessID (BusinessID) values (@ID)  insert into BusinessID (BusinessID) values (@ID)  End Try  Begin Catch  Print 'PK already exist' THROW End Catch 
like image 999
user2405574 Avatar asked May 21 '13 13:05

user2405574


People also ask

How do you throw in SQL Server?

If the THROW statement is specified without parameters, it must appear inside a CATCH block. This causes the caught exception to be raised. Any error that occurs in a THROW statement causes the statement batch to be terminated. % is a reserved character in the message text of a THROW statement and must be escaped.

Does RAISERROR stop execution?

RaisError does not end processing of a batch. All you need to do is put a Return after the RaisError and the batch will stop there. Errors with a severity of 20 or higher stop the transaction and cause an immediate disconnect.

What is the difference between Raiserror and throw in SQL Server?

According to the Differences Between RAISERROR and THROW in Sql Server: Both RAISERROR and THROW statements are used to raise an error in Sql Server. The journey of RAISERROR started from Sql Server 7.0; whereas the journey of the THROW statement has just begun with Sql Server 2012.

How does SQL Server handle out of memory exception?

Use sqlcmd Utility instead of SSMS to run the SQL queries. This method enables queries to be run without the resources that are required by the SSMS UI. Additionally, you can use the 64-bit version of Sqlcmd.exe to avoid the memory restriction that affects the 32-bit SSMS process.


1 Answers

THROW Statement is introduced in SQL Server 2012

http://msdn.microsoft.com/en-us/library/ee677615.aspx

You can use RAISERROR instead.

http://msdn.microsoft.com/en-us/library/483588bd-021b-4eae-b4ee-216268003e79(v=sql.105)

BEGIN CATCH     DECLARE @ErrorMessage NVARCHAR(4000);     DECLARE @ErrorSeverity INT;     DECLARE @ErrorState INT;      SELECT          @ErrorMessage = ERROR_MESSAGE(),         @ErrorSeverity = ERROR_SEVERITY(),         @ErrorState = ERROR_STATE();      RAISERROR (@ErrorMessage, -- Message text.                @ErrorSeverity, -- Severity.                @ErrorState -- State.                ); END CATCH; 
like image 102
Nenad Zivkovic Avatar answered Sep 26 '22 13:09

Nenad Zivkovic