两个表关联更新

来源:互联网 发布:php是什么文件格式 编辑:程序博客网 时间:2024/05/18 13:30


      在项目开发过程中,突然发现有一个表的字段值关联错误。更改过来之后,发现之前的测试数据够不能用了,好在保存的数据有关联关系。可以通过Update来更改过来。不过好久没有弄过两个表的关联更新了。有点生疏了。特地在这里记录一下。

表:

create table TA(ID int identity(1,1),R1 int,R2 int);create table TB(ID int identity(1,1),R int);

测试数据:

insert into TA select 1,11union allselect 2,12;insert into TB select 1union allselect 2;

现在TB表中的R字段关联的是TA表中的R1字段,目前需要调整为关联TA表的R2字段,更新的语句为:

 update TB  set R = TA.R2 from TB inner join TA  on TA.R1 = TB.R