我写了一个保存卡的存储过程。我想何时Barcode_Num
重复,不允许添加和显示消息(我知道必须使用 from 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