Wenn ich versuche, einen Kreditkartentyp laufen zu aktualisieren, indem Sie in der @ID und @Name vorbei ich einen Fehler, der sagt:Probleme mit IF exists()
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.
Das Problem mit meinem Stück Code ist dass überprüft das Vorhandensein der ID in der Tabelle CreditCardTypes diese Anweisung:
-- 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
Hat keine Ahnung, jemand, warum das mir einen Fehler geben kann? Ich habe viele Beispiele gesehen, die das if exists() auf diese Weise benutzen, aber aus irgendeinem Grund gibt es mir einen Fehler.
Hier ist der gesamte Prozess.
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
Oh mein Gott! Du bist super Bruder, danke für die Hilfe. Das war genau das Problem. Lektion gelernt. Danke, Mann! – Cory