error Conversion failed when converting the varchar value 'Dog' to data type int The error is on the line 40

sql

Solution

The error usually means that you are using the `+` sign for string concatenation. But one or more of the variables is numeric (in this case an integer).

By my reckoning, this is line 40:

    PRINT 'CustomerID: ' +@customerID

You might try someting like:

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

You'll probably have this problem on other lines in your code as well. Good luck.

Problem

I'm trying to create dynamic SQL statement, but I'm getting following error: Conversion failed when converting the varchar value 'Dog' to data type int The error is on the line 40 What does this error actually means and where is this error in my 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 ```

Original source