0

我有以下过程应该在多对多表中插入一个单元格。它失败的问题是错误消息,我不知道它是在哪里引起的,因为我不打算进行任何转换。

ALTER PROCEDURE [dbo].[updateGrade] 
    @grade int, 
    @studentName nvarchar(50), 
    @courseName nvarchar(50)
AS
    BEGIN TRANSACTION;

    BEGIN TRY
            IF (@grade < 0 OR @grade > 10 OR @grade=NULL )
                 RAISERROR('!! grade -- @grade < 0 OR @grade > 10 OR ISNULL(@grade, 0) = 0', 18, 0)
            IF (@studentName = '' OR ISNULL(@studentName,0) = 0)
                 RAISERROR('!! studentName -- @studentName = '' OR ISNULL(@studentName)', 18, 0)        
            IF (@courseName = '' OR ISNULL(@courseName,0) = 0)
                 RAISERROR('!! courseName -- @courseName = '' OR ISNULL(@studentName)', 18, 0)

        SET @studID = (SELECT id FROM students WHERE students.name = @studentName)
        SET @courseID = (SELECT id FROM courses WHERE courses.name = @courseName)

        INSERT INTO grades (grade, studentID, courseID)
            VALUES (@grade, @studID, @courseID)
        END TRY
        BEGIN CATCH
            SELECT 
                ERROR_NUMBER() AS ErrorNumber
                ,ERROR_SEVERITY() AS ErrorSeverity
                ,ERROR_STATE() AS ErrorState
                ,ERROR_PROCEDURE() AS ErrorProcedure
                ,ERROR_LINE() AS ErrorLine
                ,ERROR_MESSAGE() AS ErrorMessage;

            IF @@TRANCOUNT > 0
                ROLLBACK TRANSACTION;
        END CATCH;

        IF @@TRANCOUNT > 0
            COMMIT TRANSACTION;

参数:

params: 6 / Maier Bogdan / baze2 

转码:

DECLARE @return_value int

EXEC    @return_value = [dbo].[updateGrade]
        @grade = 6,
        @studentName = N'Maier Bogdan',
        @courseName = N'baze2'

SELECT  'Return Value' = @return_value

GO

错误:

将 nvarchar 值“Maier Bogdan”转换为数据类型 int 时转换失败。

4

1 回答 1

2

错误在这里:

ISNULL(@studentName,0) = 0

将@studentName (nvarchar(50)) 与 0 (int) 进行比较

请注意,当 @studentName 为 Null 时,问题不会出现,因为您将 0 与 0 进行比较,但是,当 @studentName 不为 null 时,您将 @studentName 与 0 进行比较!

于 2013-05-29T15:40:19.307 回答