1

我正在尝试创建动态 SQL 语句,但出现以下错误:

将 varchar 值 'Dog' 转换为数据类型 int 时转换失败 错误在第 40 行

这个错误实际上是什么意思,这个错误在我的 StoredProcedure 中在哪里

USE exercise

GO

CREATE PROC REPORT

@customerID VARCHAR
AS

SELECT * INTO #temp FROM customer,animal WHERE customer.customerID = @customerID

ALTER TABLE #temp

ADD

Printed SMALLINT

UPDATE #temp

SET Printed = 0

DECLARE @customerName VARCHAR (30)
DECLARE @customerTelephone VARCHAR(30)
DECLARE @animalID INT
DECLARE @animalName VARCHAR (30)
DECLARE @quantity INT
DECLARE @price INT
DECLARE @speciesName VARCHAR(40)
DECLARE @totalPrice INT

SELECT  
        @customerName = customer.customerName,

        @customerTelephone = customer.customerTelephone

        FROM customer 

        WHERE @customerID = customer.customerID

        PRINT 'CustomerID: ' +@customerID
        PRINT 'Customer Name ' +@customerName
        PRINT 'Customer Telephone: ' +@customerTelephone
        PRINT'animalID  animalName  quantity  price  Species  totalPrice'
        WHILE EXISTS (SELECT * FROM #temp WHERE Printed = 0)
        BEGIN
        SELECT @animalID = MIN(animalID) FROM #temp WHERE Printed = 0

        SELECT @animalName = animalName,
                @quantity = animalCustomer.quantity,
                @speciesName = species.specieName
                FROM animal
                 INNER JOIN animalCustomer ON animal.animalID = animalCustomer.animalIdentificater 
                INNER JOIN species ON species.specieName = animal.speciesIdentificater
                WHERE animal.animalID = @animalID
                SET @totalPrice = @price * @quantity            
                PRINT @animalID+'  '+@animalName+'  '+@quantity+'  '+@speciesName+'  '+@totalPrice          
        UPDATE #temp
                SET Printed = 1
                WHERE @animalID = animalID
        END


DROP TABLE #temp
GO
4

1 回答 1

8

该错误通常意味着您正在使用+符号进行字符串连接。但是一个或多个变量是数字的(在这种情况下是整数)。

据我估计,这是第 40 行:

    PRINT 'CustomerID: ' +@customerID

您可以尝试类似:

    PRINT 'CustomerID: ' + cast(@customerID as varchar(255))

您的代码中的其他行也可能会遇到此问题。祝你好运。

于 2013-03-27T21:43:12.367 回答