还是update动态SQL的问题,第三次了

ms222ms252288601 2009-03-26 11:00:38
数据库中有表test,列为:
id A1 A2 A3 B1 B2 B3
-----
要求用变量@id,@name,@pro,来修改“test”表
若@name=A1 and @pro=B1,则修改@id的A1值为“1”,B1值为“1”
若@name=A2 and @pro=B1,则修改@id的A2值为“1”,B1值为“1”
....
若@name=A6 and @pro=B2,则修改@id的A6值为“1”,B2值为“1”
存储过程为:
CREATE proc Utest
@id as char(10) ,
@name as char(10),
@pro as char(10)
as
begin
declare @sql as char(100)
set @sql = 'update test set ' + @name + ' = 1 ,'+@pro+'=1 where id = ' + cast(@id as varchar)
exec(@sql)
end
GO
-----
这样一次执行单行没问题。
问题是,在我的gridview中,每行数据后,都有一个checkbox,如果选中单行的话,可以修改成功。选中多行,只有第一行起作用。
我把where id = ' + cast(@id as varchar)改为:where id in ' + cast(@id as varchar)
也不行,应该怎么该?
...全文
80 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
ms222ms252288601 2009-03-27
  • 打赏
  • 举报
回复
大家来邦邦忙啊,受不了了~
ms222ms252288601 2009-03-26
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 dawugui 的回复:]
引用 3 楼 ms222ms252288601 的回复:
不要只同情,帮下忙啊,看笑话呢不是。。。
回2楼,我的意思是一次修改多行,感觉应该是where id in .....
但是改了还不对,应该是什么啊


SQL code set @sql = 'update test set ' + @name + ' = 1 ,'+@pro+'=1 where charindex(id , ''' + cast(@id as varchar) + ''')>0'
[/Quote]
谢谢你几次的回复,不过还是不行啊
效果跟 where id = ' + cast(@id as varchar) 一样,只有一条修改成功,另外一条没受影响

dawugui 2009-03-26
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 ms222ms252288601 的回复:]
不要只同情,帮下忙啊,看笑话呢不是。。。
回2楼,我的意思是一次修改多行,感觉应该是where id in .....
但是改了还不对,应该是什么啊
[/Quote]
  set @sql = 'update test set ' + @name + ' = 1 ,'+@pro+'=1 where charindex(id , ''' + cast(@id as varchar) + ''')>0'
ms222ms252288601 2009-03-26
  • 打赏
  • 举报
回复
不要只同情,帮下忙啊,看笑话呢不是。。。
回2楼,我的意思是一次修改多行,感觉应该是where id in .....
但是改了还不对,应该是什么啊
htl258_Tony 2009-03-26
  • 打赏
  • 举报
回复
CREATE proc Utest 
@id as char(10) ,
@name as char(10),
@pro as char(10)
as
begin
declare @sql as char(100)
set @sql = 'update test set ' + @name + ' = 1 ,'+@pro+'=1 where id = ''' + cast(@id as varchar) +''''
exec(@sql)
end
GO
dawugui 2009-03-26
  • 打赏
  • 举报
回复
很同情你.
ms222ms252288601 2009-03-26
  • 打赏
  • 举报
回复
哪位高手帮忙看下吧,谢谢了

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧