Link to home
Start Free TrialLog in
Avatar of CipherIS
CipherISFlag for United States of America

asked on

SQL Throw Error

I want to throw an error in my SQL SPROC but receiving an intellisense error in my code.  Basically, code is as follows
BEGIN TRY

  --Standard SQL
  IF @Length <> @PartLength THEN
      BEGIN 
	  THROW 50001, 'Your custom error message.', 1;
     END

END TRY
BEGIN CATCH

    SELECT 
        ERROR_NUMBER() AS ErrorNumber,
	ERROR_SEVERITY() AS ErrorSeverity,
	ERROR_STATE() as ErrorState,
	ERROR_PROCEDURE() as ErrorProcedure,
	ERROR_LINE() as ErrorLine,
	ERROR_MESSAGE() as ErrorMessage;    

END CATCH

Open in new window

ThrowError.png
SOLUTION
Avatar of Patrick Bogers
Patrick Bogers
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The solution provided by Patrick doesn't work.
BEGIN; will return an error!!
Sad to see those kind of reaction...
Why sad?
Do you prefer to have a comment marked as solution when it doesn't really solves an issue?
Don't forget that this question can be used in the future to solve similar issues and people will check for the marked comments and will lose time applying something that doesn't fix their issue.
Not starting a discussion here so here my last comment.

I find it sad to see Some expert shouting another experts solution is wrong because the other expert thinks he is right.
Is not like I think I'm right. I can prove that I'm right.
With BEGIN; it's says "Incorrect syntax near the keyword 'THEN'."
User generated image
As I explained in my comment, the error is in the THEN keyword that in T-SQL isn't required so the expect message will be raised:
User generated image
Experts should test their solutions before posting. It's admissible a typo but can't understand a reaction if somebody points that your solution is wrong. Everybody makes mistake so just accept it. That happened to me as well but I never reacted back and accept the call of attention.