求教一条有关update的Sql语句

来源:百度知道 编辑:UC知道 时间:2024/04/27 11:14:59
如何表A表中的数据来更新B表中符合条件的数据

如果没有一定的条件非要更新两张表,就用两个UPDATE语句好了,我还没有使用过单纯的直接更新,给你一个关联两个表的语句:
UPDATE titles
SET ytd_sales = titles.ytd_sales + sales.qty
FROM titles, sales
WHERE titles.title_id = sales.title_id
AND sales.ord_date = (SELECT MAX(sales.ord_date) FROM sales)

要么

用trigger吧

我的示例如下,至于如何去更新表的方法你自行定义。

create table a(b int)
create table b(d int)

insert into a values(100)
insert into b values(200)

create trigger for_a_b
on a
instead of update
as
begin
update b set d=d+1
end

update a set b=1000

select * from b

结果如下:

d
-----------
201

(所影响的行数为 1 行)

如何表A表中的数据来更新B表中符合条件的数据

update [B表] SET [需要更新字段]=a.[A表中相应的字段]
FROM [A表] a,[B表] b
where a.[A表中的关联列]=b.[B表中的关联列]

update atable set xcolumns = 'xx' where ycolumns in(
select ycolumns from b
)

子查询?HO