日期:2014-05-17 浏览次数:20511 次
create table T1(aid int, aname varchar(5)) create table T2(bid int, bname varchar(5)) insert into T1 values(1,'a'),(3,'b'),(10,'c') insert into T2 values(1,'aa'),(1,'aa'),(3,'b'),(10,'c'),(10,'c') with t as ( select aid,aname, row_number() over(order by aid) rn from t1 ) update b set b.bid=a.rn from T2 b inner join t a on b.bid=a.aid; with t as ( select aid,aname, row_number() over(order by aid) rn from T1 ) update a set a.aid=b.rn from T1 a inner join t b on a.aid=b.aid; select * from T1 /* aid aname ----------- ----- 1 a 2 b 3 c (3 row(s) affected) */ select * from T2 /* bid bname ----------- ----- 1 aa 1 aa 2 b 3 c 3 c (5 row(s) affected) */