为何update语句不起作用?

tomsmish 2009-04-19 11:55:07
有一张表table1
id name
-------------
1 A
2 B
3 C
4 D

update table1 set name=(select name from table1 where id=4) where id=2
就是想把第4行的name值赋给第2行,执行语句之后显示影响行数是1,但第二行的name值怎么还是B而不是D?
...全文
322 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
lg3605119 2009-04-20
  • 打赏
  • 举报
回复
语法正确
kkk33181102 2009-04-20
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 fcuandy 的回复:]
语句没有问题的,不过不建议这么写。


update a set a.name = b.name
from table1 a
, table1 b
where a.id=2 and b.id=4
[/Quote]
同意楼上的,当查询出多个结果集是会报错的
鑫辰软件 2009-04-19
  • 打赏
  • 举报
回复
多半上表上有触发器造成的,你的SQL语句是正确的,你可以新建一个数据库试试看。
ggsysy 2009-04-19
  • 打赏
  • 举报
回复
alter table table1 disable trigger all
update table1 set name=(select name from table1 where id=4) where id=2
alter table table1 enable trigger all
  • 打赏
  • 举报
回复
看看表上是否有触发器或者其他的约束之类的没有?
ginni215 2009-04-19
  • 打赏
  • 举报
回复
如果用的是ORACLE,更新完,关闭然后重新查一下看看~
長胸為富 2009-04-19
  • 打赏
  • 举报
回复
语句没有问题,看看有没有触发器什么的阻止了
ws_hgo 2009-04-19
  • 打赏
  • 举报
回复
语法正确
taoistong 2009-04-19
  • 打赏
  • 举报
回复





create table td
(id int, name char(1))
-------------
insert into td select
1 ,'A'
insert into td select
2 ,'B'
insert into td select
3 ,'C'
insert into td select
4 ,'D'
update td set name=(select name from td where id=4) where id=2
select * from td



不可能 , 你再好好的测试一下
wzy_love_sly 2009-04-19
  • 打赏
  • 举报
回复
create table tb(id int,name varchar(10))
insert into tb select 1,'A'
insert into tb select 2,'B'
insert into tb select 3,'C'
insert into tb select 4,'D'


update TB set name=(select name from TB where id=4) where id=2

SELECT * FROM TB


id name
1 A
2 D
3 C
4 D

变了啊
fcuandy 2009-04-19
  • 打赏
  • 举报
回复
语句没有问题的,不过不建议这么写。


update a set a.name = b.name
from table1 a
, table1 b
where a.id=2 and b.id=4

22,210

社区成员

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

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