我写了一个保存卡片的存储过程。我想当Barcode_Num
重复时,不允许添加和显示消息(我知道必须从RAISERROR
使用),但我想设置RAISERROR
的数字并设置消息,即:
卡号 10000001(ex) 重复,您不允许添加此卡号
我设置了这个但没有奏效。
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
如果要使用 RAISERROR
的格式化工具,参数位于严重性和状态参数之后:
RAISERROR('the card number %d is duplicate',16,1,@CardID)