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
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.
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.
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.
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.
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;
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