Hi Friends,
Continuing with my DENALI series, today I would like to talk about the new THROW statement. T-SQL TRY/CATCH were available since SQL Server 2005 but THROW and FINALLY were missing. We have THROW now, but FINALLY is yet to come 🙂
The syntax as follows:
--THROW [ { error_number | @local_variable }, -- { message | @local_variable }, -- { state | @local_variable } -- ] [ ; ]
A simple example could be as follows:
Another example of THROW, this time inside CATCH block:
USE tempdb; GO CREATE TABLE dbo.TestThrow ( custID INT PRIMARY KEY ); BEGIN TRY INSERT INTO dbo.TestThrow VALUES (1); -- Primary key violation, error number INSERT dbo.TestThrow(custID) VALUES(1); END TRY BEGIN CATCH PRINT 'Now in catch block.'; THROW; END CATCH;
Output:
There are some notable differences between RIASERROR & THROW which I shall cover in another blog. I have also heard some people talking about deprecation of RAISERROR but I am not sure on this. (However, I don’t think that should be happening)
Interesting and thanks for taking time to share!!