我在存储过程里使用下面语句update table1 set a = @a where id in ( @id )其中@id是 Nvarchar(1000)我传过去的字符串为 1,2,3,4报的错误是不能将 '1,2,3,4' 转化为数字型是不是传输的字符串自动给加上 '' 这个符号?请问,我该如何处理?谢谢各位。
exec('update table1 set a = @a where id in ('+@id+')')
update table1 set a=@a where charindex(','+rtrim(id)+',' , ','+@id+',')
exec('update table1 set a = '+''''+@a+''''+' where id in ( '+@id+' )')
我也是用下面的语句的 exec('update table1 set a ='+@a+' where id in ('+@id+')') 因为我需要更新的比较多,就是 a ='+@a+' 这个会很多, 但是,结果老是报 '31' 附近有语法错误。可是我查遍了传过去的值,也没有一个是 '31' 的啊请教了
楼上采取charindex方法的不错。
个人不喜欢用charindex 我更喜欢用replace函数SQL codeexec('update table1 set a = @a where id in ('''+ replace(@id,',',''',''' )+''' ')
update table1 set a=@a where charindex(','+rtrim(id)+',' , ','+@id+',') 最喜欢的就是这种了!!
print 'update table1 set a ='+@a+' where id in ('+@id+')'这句话,看看
update table1 set a=@a where charindex(','+rtrim(id)+',' , ','+@id+',') 支持这个
exec('update table1 set a ='+quotename(@a, '''')+' where id in ('+@id+')')
update table1 set a = @a where id in ( SELECT LTRIM(RTRIM(B.x.value('.','int'))) FROM( SELECT CONVERT(XML,'<v>'+REPLACE(@id,',','</v><v>')+'</v>') AS s ) AS A CROSS APPLY A.s.nodes('v') AS B(x) )
update table1 set a=@a where charindex(','+rtrim(id)+',' , ','+@id+',')
update table1 set a=@a where charindex(','+rtrim(id)+',' , ','+@id+',')能帮忙解释下么? charindex执行后返回 int类型 跟在where语句后面
update table1 set a=@a where charindex(','+rtrim(id)+',' , ','+@id+',') >0 应该是这样的!@
我也是用下面的语句的
exec('update table1 set a ='+@a+' where id in ('+@id+')') 因为我需要更新的比较多,就是 a ='+@a+' 这个会很多,
但是,结果老是报 '31' 附近有语法错误。可是我查遍了传过去的值,也没有一个是 '31' 的啊请教了
我更喜欢用replace函数SQL codeexec('update table1 set a = @a where id in ('''+ replace(@id,',',''',''' )+''' ')
最喜欢的就是这种了!!
update table1 set a=@a where charindex(','+rtrim(id)+',' , ','+@id+',') 支持这个
(
SELECT LTRIM(RTRIM(B.x.value('.','int')))
FROM(
SELECT CONVERT(XML,'<v>'+REPLACE(@id,',','</v><v>')+'</v>') AS s
) AS A
CROSS APPLY A.s.nodes('v') AS B(x)
)
update table1 set a=@a where charindex(','+rtrim(id)+',' , ','+@id+',')
跟在where语句后面
update table1 set a=@a where charindex(','+rtrim(id)+',' , ','+@id+',') >0
应该是这样的!@