Thomas Andreè Wang
Thomas Andreè Wang

Reputation: 3429

RAISERROR conversion from SQL Server 8 to 11

I have moved a database form a old legacy MSSQL server 2000 (SQL Server 8) to SQL Server 11 and im receiving some errors in several triggers.

They are formed like this.

RAISERROR 20001 @msg

and

RAISERROR 44444 'Field ''comp_v1'' cannot contain a null value.'

so after some reading i see that this is a known change, but i cant find any good known solutions.

At first i thought to just replace them to something like this

RAISERROR ('Field ''comp_v1'' cannot contain a null value.',16,-1)

The issue here is, well i have no real idea on what effect this has, and if there is any good straightforward conversion from the old line to a new line.

Im assuming that there is a reason for the codes 44444 and 20001 etc.

Upvotes: 1

Views: 457

Answers (1)

Rahul Tripathi
Rahul Tripathi

Reputation: 172628

I would advise you to use THROW keyword to throw the error. You can check this for reasons:

With THROW the benefit is: it is not mandatory to pass any parameter to raise an exception. Just using the THROW; statement will get the error details and raise it

whereas with RAISERROR

With RAISERROR developers had to use different ERROR_xxxx() system functions to get the error details to pass through the RAISERROR() statement, like:

  • ERROR_NUMBER()
  • ERROR_MESSAGE()
  • ERROR_SEVERITY()
  • ERROR_STATE()

Upvotes: 1

Related Questions