Conversion failed when converting the nvarchar value … to data type int

前端 未结 7 1353
我寻月下人不归
我寻月下人不归 2020-12-03 20:54

I created the procedure listed below:

CREATE procedure getdata
(
    @ID int,
    @frm varchar(250),
    @to varchar(250)
)
AS
BEGIN

DECLARE @SQL nvarchar(5         


        
相关标签:
7条回答
  • 2020-12-03 21:02

    You are trying to concatenate a string and an integer.
    You need to cast @ID as a string.
    try:

    SET @sql=@sql+' AND Emp_Id_Pk=' + CAST(@ID AS NVARCHAR(10))
    
    0 讨论(0)
  • 2020-12-03 21:04

    I have faced to the same problem, i deleted the constraint for the column in question and it worked for me. You can check the folder Constraints.

    Capture :

    0 讨论(0)
  • 2020-12-03 21:13

    You got this Error because you tried to convert column DataType from String to int which is

    leagal if and only if

    you dont have row in that table with string content inside that column

    so just make sure your previously inserted Rows is compatible with the new changes

    0 讨论(0)
  • 2020-12-03 21:17

    I was using a KEY word for one of my columns and I solved it with brackets []

    0 讨论(0)
  • I use the latest version of SSMS or sql server management studio. I have a SQL script (in query editor) which has about 100 lines of code. This is error I got in the query:

    Msg 245, Level 16, State 1, Line 2
    Conversion failed when converting the nvarchar value 'abcd' to data type int.
    

    Solution - I had seen this kind of error before when I forgot to enclose a number (in varchar column) in single quotes.

    As an aside, the error message is misleading. The actual error on line number 70 in the query editor and not line 2 as the error says!

    0 讨论(0)
  • 2020-12-03 21:24

    Try Using

    CONVERT(nvarchar(10),@ID)
    

    This is similar to cast but is less expensive(in terms of time consumed)

    0 讨论(0)
提交回复
热议问题