SQL - When to use @Variable

66 Views Asked by At

I found this piece of code online:

...
END TRY
BEGIN CATCH
 DECLARE
   @ErMessage NVARCHAR(2048),
   @ErSeverity INT,
   @ErState INT
 
 SELECT
   @ErMessage = ERROR_MESSAGE(),
   @ErSeverity = ERROR_SEVERITY(),
   @ErState = ERROR_STATE()
 
 **RAISERROR (@ErMessage,
             @ErSeverity,
             @ErState )**
END CATCH

My Questions is: In the CATCH block, why did the author have to declare the arguments with @ErMessage, @ErServerity, @ErState and not just input:

RAISERROR (ERROR_MESSAGE(), ERROR_SEVERITY(), ERRORSTATE())

1

There are 1 best solutions below

0
KekuSemau On

For SQL Server, this has been discussed here (msdn) (a bit older).

In short: Microsoft just hasn't implemented full evaluation of expressions as parameters.
Also, allowing statements without semicolons and some context-dependent keywords would make it more complicated.