When I try to run update a credit card type by passing in the @ID and @Name I get an error that says:
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.
The problem is with my piece of code that checks for the existence of the id in the CreditCardTypes table using this statement:
-- 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
Does anyone have any idea why this may be giving me an error? I have seen many examples of using the the if exists() in this way, but for some reason it’s giving me an error.
Here is the entire proc.
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
Change:
To:
%sis used for substituting strings… but%dis the substitution parameter for ints.RAISERROR in MSDN