2012-03-28 51 views
1

當我嘗試運行通過傳遞@ID和@Name更新信用卡類型,我得到一個錯誤,指出:問題與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. 

的問題是我的一段代碼對於該ID在CreditCardTypes表使用此語句存在檢查:

-- 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是整數的替代參數。

RAISERROR in MSDN

+0

哦,我的地獄!你真棒兄弟感謝你的幫助。這正是問題所在。學過的知識。謝啦! – Cory 2012-03-28 16:24:22