34,594
社区成员
发帖
与我相关
我的任务
分享
--> 测试数据: [A]
if object_id('[A]') is not null drop table [A]
create table [A] (Aid int,aa int)
insert into [A]
select 1,0 union all
select 2,0 union all
select 3,0
--> 测试数据: [B]
if object_id('[B]') is not null drop table [B]
create table [B] (Bid int,bb int)
insert into [B]
select 1,1 union all
select 2,2 union all
select 3,3 union all
select 4,4 union all
select 5,5 union all
select 6,6 union all
select 7,7
--2000
if object_id('tempdb..#') is not null drop table #
go
create table #(id int identity ,bb int)
insert into #(bb) select bb from (select distinct bb from b) t order by newid()
update a set aa=bb from a,# t where a.aid=t.id
select * from a
drop table #
drop table a
drop table b
update A Set AA=(Select top 1 BB from B WHERE a.Aid<>b.Bid Order by newid())
如果ID非常无序,改为这样处理。--> 测试数据: [A]
if object_id('[A]') is not null drop table [A]
go
create table [A] (Aid int,aa int)
insert into [A]
select 1,0 union all
select 2,0 union all
select 3,0
--> 测试数据: [B]
if object_id('[B]') is not null drop table [B]
go
create table [B] (Bid int,bb int)
insert into [B]
select 1,1 union all
select 2,2 union all
select 3,3 union all
select 4,4 union all
select 5,5 union all
select 6,6 union all
select 7,7
update A Set AA=(Select top 1 BB from B WHERE a.Aid<=b.Bid Order by newid())
select * from a
/*
Aid aa
----------- -----------
1 7
2 6
3 5
(3 行受影响)
*/
1楼的这样改一下也没问题。--> 测试数据: [A]
if object_id('[A]') is not null drop table [A]
create table [A] (Aid int,aa int)
insert into [A]
select 1,0 union all
select 2,0 union all
select 3,0
--> 测试数据: [B]
if object_id('[B]') is not null drop table [B]
create table [B] (Bid int,bb int)
insert into [B]
select 1,1 union all
select 2,2 union all
select 3,3 union all
select 4,4 union all
select 5,5 union all
select 6,6 union all
select 7,7
update A Set AA=B.BB
from A
join(select row_number() over(order by newid()) id ,BB from B ) B on A.Aid=B.ID
select * from a
(3 行受影响)
Aid aa
----------- -----------
1 7
2 2
3 4
(3 行受影响)
--AA可重复
update A1 set A1.AA=B.BB from
A A1 cross apply(
select top 1 Aid,B.BB from a,B where A.Aid=A1.AID
order by newid()
) B
select * from A
(3 行受影响)
Aid aa
----------- -----------
1 2
2 2
3 5
(3 行受影响)
create table a(aid int, aa int);
create table b(bid int, bb int);
insert into a(aid, aa)
select
1,0 union all select
2,0 union all select
3,0;
insert into b(bid,bb)
select
1,1 union all select
2,2 union all select
3,3 union all select
4,4 union all select
5,5 union all select
6,6 union all select
7,7;
update a
set a.aa = b1.bb
from a inner join (select row_number() over(order by newid()) as bid, bb from b ) b1
on a.aid=b1.bid;
select * from a;
--> 测试数据: [A]
if object_id('[A]') is not null drop table [A]
create table [A] (Aid int,aa int)
insert into [A]
select 1,0 union all
select 2,0 union all
select 3,0
--> 测试数据: [B]
if object_id('[B]') is not null drop table [B]
create table [B] (Bid int,bb int)
insert into [B]
select 1,1 union all
select 2,2 union all
select 3,3 union all
select 4,4 union all
select 5,5 union all
select 6,6 union all
select 7,7
update A Set AA=B.BB
from A
join(select row_number() over(order by newid()) id ,BB from B ) B on A.Aid=B.ID
select * from a
update A Set AA=(Select top 1 BB from B Order by newid())