SQL Server Conversion failed: when converting the nvarchar value to data type int

ie3xauqp  于 2023-11-16  发布在  其他
关注(0)|答案(3)|浏览(214)

I create a stored procedure

  1. CREATE PROCEDURE [dbo].[wsp_Display_Invoice]
  2. @DOID NVARCHAR(50),
  3. @brcode INT
  4. AS
  5. BEGIN
  6. SELECT DISTINCT
  7. A.Item_Code,
  8. (B.Description + '-' + B.Product_Code + '(' + D.Brand_Name+ ')') AS Description,
  9. A.Quantity, B.UOM, A.Rate, A.DOID,
  10. C.Doc_No, B.Tax_Percentage AS Tax, B.Tax_Percentage AS Tax1,
  11. F.QuotationID
  12. FROM
  13. DO_T A
  14. INNER JOIN
  15. inv_Item_Master B ON A.Item_Code = B.Item_Code
  16. INNER JOIN
  17. DO C ON A.DOID = C.DOID
  18. INNER JOIN
  19. Inv_Brand D ON D.Brand_ID = B.Brand_ID
  20. INNER JOIN
  21. Quotation_T F ON F.DOID = A.DOID
  22. WHERE
  23. A.DOID LIKE '%' + @DOID + '%'
  24. AND a.BR_Code = @brcode
  25. END

Got a error

Conversion failed when converting the nvarchar value '22,23' to data type int.

8zzbczxx

8zzbczxx1#

Your code is doing an implicit conversion somewhere. The obvious places are the string concatenation with + . If any of the arguments is a number, then + is interpreted as numeric addition -- and you get an error.

Less obvious places are other operations, such as JOIN conditions. If one column is a string and the other a number, then SQL Server attempts to convert the string to a number -- and you can get an error.

So, I would try the following.

Replace:

  1. (B.Description + '-' + B.Product_Code + '(' + D.Brand_Name+ ')') AS Description,

with:

  1. (B.Description + '-' + CAST(B.Product_Code as VARCHAR(255)) + '(' + D.Brand_Name+ ')') AS Description,

This is only necessary if Product_Code is a number.

  1. A.DOID LIKE '%' + @DOID + '%'

with:

  1. CAST(A.DOID as VARCHAR(255)) LIKE '%' + @DOID + '%'

This is only necessary of A.DOID is a number.

Change the type of @brcode to a string. This is only necessary if a_BR_CODE is a number.

If these do not work, you will have to look at the JOIN conditions to see if any of them mix types.

展开查看全部
vc6uscn9

vc6uscn92#

I also think that we need to know the data types of the DOID fields in the various table. My guess is that there is a data type mismatch on one of the joins.

wn9m85ua

wn9m85ua3#

try the following:

  1. CREATE PROCEDURE [dbo].[wsp_Display_Invoice]
  2. @DOID NVARCHAR(50),
  3. @brcode INT
  4. AS
  5. BEGIN
  6. SELECT DISTINCT
  7. A.Item_Code,
  8. (B.Description + '-' + B.Product_Code + '(' + D.Brand_Name+ ')') AS Description,
  9. A.Quantity, B.UOM, A.Rate, A.DOID,
  10. C.Doc_No, B.Tax_Percentage AS Tax, B.Tax_Percentage AS Tax1,
  11. F.QuotationID
  12. FROM
  13. DO_T A
  14. INNER JOIN
  15. inv_Item_Master B ON A.Item_Code = B.Item_Code
  16. INNER JOIN
  17. DO C ON A.DOID = C.DOID
  18. INNER JOIN
  19. Inv_Brand D ON D.Brand_ID = B.Brand_ID
  20. INNER JOIN
  21. Quotation_T F ON F.DOID = A.DOID
  22. WHERE
  23. CONVERT(varchar(50), A.DOID) LIKE '%' + @DOID + '%'
  24. AND a.BR_Code = @brcode
  25. END
展开查看全部

相关问题