1

I already created the error in sys.message. The problem is when I add it to my stored procedure, it doesn't pass me the message back. The stored procedure checks to see if an id exists in a certain areacode if the id does not exists the raiserror should be fired.

AS
BEGIN 
    DECLARE @Result as int 

    IF EXISTS(SELECT ID, areacode
              FROM Table1 
              WHERE ID = @ID
                AND areacode = @areacode)
        RAISERROR (50030, 1, 1)

BEGIN  
   INSERT INTO Table2 ( //columns go here )
   VALUES ( //values for columns )
END
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
doe
  • 148
  • 4
  • 25
  • It's not clear if you except the error to be raised when the id is not found as you wrote in your description, or if it is found as written in the stored procedure. – Zohar Peled Apr 13 '15 at 07:46
  • if its not found it should display the error – doe Apr 13 '15 at 07:48

2 Answers2

2

Your severity is to low, try to set it a bit higher: raiserror (50030,16,1)

Btw. why do you try to raise the error right before your insert?

https://msdn.microsoft.com/en-us/library/ms178592.aspx

  • 1
    First he needs to raise the error under the right conditions. Second, read this: http://stackoverflow.com/questions/1122925/what-do-the-different-raiserror-severity-levels-mean – Zohar Peled Apr 13 '15 at 07:54
  • Thanks i changed it to 16.sorry i couldnt mark two answers,but very helpfull – doe Apr 13 '15 at 08:53
2
DECLARE @Result as int 

IF EXISTS(SELECT 1 
      FROM Table1 
      WHERE ID=@ID
      AND areacode=@areacode
      )
BEGIN  
    INSERT INTO Table2 (/* columns go here */)
    VALUES (/* values for columns */)
END ELSE BEGIN
    RAISERROR (50030,1,1)
END
Zohar Peled
  • 79,642
  • 10
  • 69
  • 121