我正在尝试创建动态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
Run Code Online (Sandbox Code Playgroud)
该错误通常意味着您使用+符号进行字符串连接.但是一个或多个变量是数字(在这种情况下是整数).
据我估算,这是第40行:
PRINT 'CustomerID: ' +@customerID
Run Code Online (Sandbox Code Playgroud)
你可能会尝试像:
PRINT 'CustomerID: ' + cast(@customerID as varchar(255))
Run Code Online (Sandbox Code Playgroud)
您可能也会在代码中的其他行上遇到此问题.祝好运.
| 归档时间: |
|
| 查看次数: |
10542 次 |
| 最近记录: |