update B set d=(select sum(b) from A where a=1) where c=2
错误提示:操作必须是一个可更新的查询。 但是如果把update B set d=(select sum(b) from A where a=1) where c=2中的(select sum(b) from A where a=1)换成一个固定的数值的话,比如1,那么可以成功的。 能不能再想想办法,拜托了!
--将A表的a字段值=1的b字段的和插入到B表中c字段=2的d字段中 create table A (a int,b int); insert into A values(1,3) insert into A values(1,4) insert into A values(1,5) insert into A values(2,3) insert into A values(3,6)create table B (c int,d int); insert into B values(1,1) insert into B values(2,2) insert into B values(2,3) insert into B values(3,4) insert into B values(4,5) --drop table A --drop table Bupdate B set d=(select sum(b) from A where a=1) where c=2select * from B where c=2c d ----------- ----------- 2 12 2 12(所影响的行数为 2 行)这个语句本身是没问题的,你的那个报错可能是其他的问题,如果要用一个值代替那个语句,可以用一个变量先把b的和取出来declare @sumb int; select @sumb=sum(b) from A where a=1 update B set d=@sumb where c=2select * from B where c=2c d ----------- ----------- 2 12 2 12(所影响的行数为 2 行)
update B set d=(select sum(b) from A where a=1) where c=2
但是如果把update B set d=(select sum(b) from A where a=1) where c=2中的(select sum(b) from A where a=1)换成一个固定的数值的话,比如1,那么可以成功的。
能不能再想想办法,拜托了!
--将A表的a字段值=1的b字段的和插入到B表中c字段=2的d字段中
create table A (a int,b int);
insert into A values(1,3)
insert into A values(1,4)
insert into A values(1,5)
insert into A values(2,3)
insert into A values(3,6)create table B (c int,d int);
insert into B values(1,1)
insert into B values(2,2)
insert into B values(2,3)
insert into B values(3,4)
insert into B values(4,5)
--drop table A
--drop table Bupdate B set d=(select sum(b) from A where a=1) where c=2select * from B where c=2c d
----------- -----------
2 12
2 12(所影响的行数为 2 行)这个语句本身是没问题的,你的那个报错可能是其他的问题,如果要用一个值代替那个语句,可以用一个变量先把b的和取出来declare @sumb int;
select @sumb=sum(b) from A where a=1
update B set d=@sumb where c=2select * from B where c=2c d
----------- -----------
2 12
2 12(所影响的行数为 2 行)