I am unable to use THROW SQL Server 2008 R2

user2405574 picture user2405574 · May 21, 2013 · Viewed 24.9k times · Source

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

Answer

Nenad Zivkovic picture Nenad Zivkovic · May 21, 2013

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;