在一个表中,用一个字段来更新另外一个字段很方便,但是有的时候另外一个字段的数据是来自视图或是另外一个表,这时候就要用到跨表更新了,下面我模拟一个这个过程:
表一:
if object_id('sdx1') is not null
drop table sdx1
go
create table sdx1(
id int identity(1,1) not null,
num int null
)
go
insert into sdx1(num) select 1
union all select 2
union all select 3
union all select 4
go表二:
if object_id('sdx2') is not null
drop table sdx2
go
create table sdx2(
id int identity(1,1) not null,
num int null
)
go
insert into sdx2(num) select 10
union all select 11
union all select 12
go表建好了看一下数据:
select * from sdx1 select * from sdx2
运行结果:

准备工作做好了,现在我们用sdx2中的num+1 来更新sdx1中的num
update sdx1 set sdx1.num=sdx2.num+1 from sdx2 where sdx2.id=sdx1.id
表建好了看一下数据:
select * from sdx1 select * from sdx2
运行结果:
