求教!关于存储过程的变量
Tb TbA
uid bigint
uname nvarchar
Tb TbC
cid nvarchar
cname nvarchar
从A取数据 插入到C
web页取的id(checkbox)列表,并调整为字符串 strids = "1,2,3,4,5,6,7",传入存储过程
存储过程主要代码:
...
@ids nvarchar(2550)(input参数)
...
INSERT into TbC
(cid,cname)
SELECT
CONVERT(nvarchar(255),uid),uname
FROM TbA
where uid IN (@ids)
提示不能将nvarchar 转为 bigint 错误处:where uid IN (@ids)
但是我用另一张表(ID是字符类型的)以同样方式插入到C,是可以的,请问是什么原因?
请高手们指点了!多谢多谢! sql 存储过程
[解决办法]
declare @sql nvarchar(max)
set @sql=
'INSERT into TbC
(cid,cname)
SELECT
CONVERT(nvarchar(255),uid),uname
FROM TbA
where uid IN ('+@ids+')'
exec(@sql)
[解决办法]
try this,
-- input参数
@ids nvarchar(2550)
.
.
.
declare @tsql varchar(6000)
select @tsql='insert into TbC(cid,cname) '
+' select convert(nvarchar(255),uid),uname '
+' from TbA '
+' where uid in('+@ids+') '
exec(@tsql)