Domanda

I wrote a stored procedure that saves card. I want to when Barcode_Num is duplicate, don't allow for adding and display message (I know that must use from RAISERROR), but I want set the number for RAISERROR and set message that is:

the card number 10000001(ex) is duplicate , and you don't allow add this

I set this but didn't worked.

ALTER PROCEDURE  [dbo].[SaveCards]
    @Barcode_Num int
    ,@Card_Status_ID int 
    ,@Card_Type_ID int
    ,@SaveDate varchar(10)
    ,@Save_User_ID int 

AS
BEGIN
  BEGIN TRAN Entrance
    Begin Try 
    if (select COUNT(*) from TBL_Cards where Barcode_Num = @Barcode_Num ) = 0
    begin
    INSERT INTO [Parking].[dbo].[TBL_Cards]
               ([Barcode_Num]
               ,[Card_Status_ID]
               ,[Card_Type_ID]
               ,[Save_Date]        
               ,[Save_User_ID])

         VALUES
               (@Barcode_Num
               ,@Card_Status_ID
               ,@Card_Type_ID
               ,@SaveDate
               ,@Save_User_ID
               )
          end
             else
                 begin 
                 --
                 declare @EndCardID int ;
             set @EndCardID= (  select Barcode_Num  from TBL_Cards  WHERE TBL_Cards.Card_ID = (SELECT MAX(Card_ID)
                             FROM TBL_Cards  ))

                 declare @CardID int ;
                 set @CardID= ( select Barcode_Num  from TBL_Cards  where Barcode_Num = @Barcode_Num)

                    --RAISERROR('قبلا کارتی ب اين شماره ثبت شده است.',50000,1)
                RAISERROR((N'the card number %d is duplicate',@CardID),50000,1);
             end   
        COMMIT TRAN Entrance
        END TRY

        Begin CATCH
            ROLLBACK TRAN Entrance
            DECLARE @Error_Number   int
            SET @Error_Number = ERROR_NUMBER()
            DECLARE @Error_Message  varchar(max)
            SET @Error_Message = ERROR_MESSAGE()
            DECLARE @Log_Error_ID   int
            EXEC [LogError] @Error_Number, @Error_Message OUTPUT, @Log_Error_ID, @Save_User_ID

            IF @Error_Message IS NOT NULL
                RAISERROR(@Error_Message,16,1)
            ELSE
                RAISERROR((N'the card number %d is duplicate',@CardID),16,1)

            END CATCH   
    END 

    Begin CATCH
            ROLLBACK TRAN Entrance
            DECLARE @Error_Number   int
            SET @Error_Number = ERROR_NUMBER()
            DECLARE @Error_Message  varchar(max)
            SET @Error_Message = ERROR_MESSAGE()
            DECLARE @Log_Error_ID   int
            EXEC [LogError] @Error_Number, @Error_Message OUTPUT, @Log_Error_ID, @Save_User_ID

            IF @Error_Message IS NOT NULL
                RAISERROR(@Error_Message,16,1)
            ELSE
                RAISERROR('قبلا کارتی با اين شماره ثبت شده است',16,1)

            END CATCH   

    END  
È stato utile?

Soluzione

If you want to use RAISERRORs formatting facilities, the arguments come after the severity and state parameters:

RAISERROR('the card number %d is duplicate',16,1,@CardID)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top