当我尝试通过传递@ID和@Name来运行更新信用卡类型时,我收到一条错误消息:
Msg 2786, Level 16, State 1, Procedure sp_SaveCreditCardType, Line 29
The data type of substitution parameter 1 does not match the expected type of the format specification.
问题在于我的代码片段使用以下语句检查CreditCardTypes表中是否存在id:
-- make sure the ID is a valid number
IF NOT EXISTS (SELECT * FROM CreditCardTypes WHERE ID = @ID)
BEGIN
RAISERROR('The Credit Card ID ''%s'' does not exist. Update Failed.', 15, 1, @ID)
RETURN -100
END
有没有人知道为什么这可能会给我一个错误?我已经看到很多以这种方式使用if exists()的例子,但由于某种原因它给了我一个错误。
这是整个过程。
CREATE PROCEDURE dbo.sp_SaveCreditCardType
(
@ID int = null,
@Name varchar(50),
@Description varchar(150) = null
)
AS
DECLARE
@Err INT
BEGIN
SET NOCOUNT ON
-- check to make sure a Name was passed in
IF @Name IS NULL
BEGIN
RAISERROR('A Name was not specified. Execution aborted.', 15, 1, @Name)
RETURN -100
END
-- check to see if an ID is passed
IF @ID IS NOT NULL AND @ID <> 0
BEGIN
-- make sure the ID is a valid number
IF NOT EXISTS (SELECT * FROM CreditCardTypes WHERE ID = @ID)
BEGIN
RAISERROR('The Credit Card ID ''%s'' does not exist. Update Failed.', 15, 1, @ID)
RETURN -100
END
-- update an existing credit card type
UPDATE CreditCardTypes
SET Name = @Name,
[Description] = @Description
WHERE ID = @ID
SET @Err = @@ERROR
IF @Err <> 0 GOTO ErrorHandler
END
ELSE
BEGIN
-- first check to make sure the credit card type doesn't already exist
IF NOT EXISTS (SELECT * FROM CreditCardTypes WHERE Name = @Name)
BEGIN
-- insert a new credit card type
INSERT INTO CreditCardTypes (Name, [Description])
VALUES (@Name, @Description)
SET @Err = @@ERROR
IF @Err <> 0 GOTO ErrorHandler
END
ELSE
RAISERROR('The Credit Card Type ''%s'' already exists. Insert failed.', 15, 1, @Name)
RETURN -100
END
SET @Err = @@ERROR
IF @Err <> 0 GOTO ErrorHandler
RETURN 0
ErrorHandler:
RAISERROR('An error occured while saving the credit card type ''%s''', 16, 1, @Name) WITH LOG
RETURN -100
END
GO
答案 0 :(得分:2)
变化:
RAISERROR('The Credit Card ID ''%s'' does not exist. Update Failed.', 15, 1, @ID)
要:
RAISERROR('The Credit Card ID ''%d'' does not exist. Update Failed.', 15, 1, @ID)
%s
用于替换字符串...但%d
是整数的替换参数。