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

前端 未结 7 1354
我寻月下人不归
我寻月下人不归 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:29

    don't use string concatenation to produce sql, you can use sp_executesql system stored prcedure to execute sql statement with parameters

    create procedure getdata @ID int, @frm varchar(250), @to varchar(250) as
    begin
        declare @sql nvarchar(max), @paramDefs nvarchar(max);
    
        set nocount on;
    
        set @sql = N'select EmpName, Address, Salary from Emp_Tb where @id is null or Emp_Id_Pk = @id';
        set @paramDefs = N'@id int';
        execute sp_executesql @sql, @paramDefs, @id = @ID;
    end
    

    see sp_executesql

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