2012-03-28 6 views
1

私は@IDと@Nameに渡すことで、クレジットカードの種類を更新実行しようとすると、私が言うエラーが出る:問題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. 

問題は、コードの私の作品であるが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 

答えて

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はint型の置換パラメータです。

RAISERROR in MSDN

+0

Oh my hell!助けを借りてくれてありがとう。それがまさに問題でした。学んだ教訓。ありがとう! – Cory