SQL 存储过程 where in 的问题 带参数的!!

来源:百度知道 编辑:UC知道 时间:2024/06/17 11:46:54
@id 是VARCHAR @name 是VARCHAR @id 格式是 3,1,2
uid 是Int 主键
写法一

declare @SelectText varchar(1000)

select @SelectText = 'update ffzs_unit set unitname='+@name+' where uid in ('+@id+')'

execute( @SelectText)

错误: 如果 @name 是 “条”的 话
错误信息:列名 '条' 无效。

方法二
update ffzs_unit set unitname=@name where uid in (@id)
错误: varchar 值 ‘3,1,2’转换为数据类型为 int 的列时发生语法错误

求:
怎么才能正常更新!!!!!!!
方法二肯定不行,@id是字符串不是集合
方法一中要这样写:
set @SelectText = 'update ffzs_unit set unitname='''+@name+''' where uid in ('+@id+')'
对于SQL中,字符串要有单引号
回答者:curllion - 同进士出身 六级 4-7

这个也不可以!
'''+@name+'''
引号不明确。测试了任何加引号情况都不行

方法二肯定不行,@id是字符串不是集合
方法一中要这样写:
set @SelectText = 'update ffzs_unit set unitname='''+@name+''' where uid in ('+@id+')'
对于SQL中,字符串要有单引号

你不要execute( @SelectText)
先改成 select @SelectText
看看输出的字符串到底是什么,然后在查询分析器中测试一下语法,再改原有的.

name是关健字