日期:2014-05-18  浏览次数:20449 次

存储过程插入int数据
将一条数据插入sqlserver中的一张表中,参数中有int类型数据时报错,存储过程如下:
create proc proc_add_p1 @id int
as
  declare @sSql1 nvarchar(4000)
  set @sSql1 = 'insert into Customers' 
  +'(CustomerID)'+' values'+'('+@id+')'
  EXECUTE sp_executesql @sSql1
若参数为varchar则没问题,为int时总是报错
Msg 245, Level 16, State 1, Procedure proc_add_p1, Line 4
Conversion failed when converting the varchar value 'insert into Customers(CustomerID) values(' to data type int.
请各位高手帮忙看看怎么写


------解决方案--------------------
set @sSql1 = 'insert into Customers'
+'(CustomerID)'+' values'+'('+@id+')'


======>>>>


set @sSql1 = 'insert into Customers'
+'(CustomerID)'+' values'+'('+rtrim(@id)+')'

------解决方案--------------------
因为 你用到 + 号连接了,默认 + 连接是字符串,你得把 @id 转一下。
cast(@id as varchar)