将A表某字段值随机插入B表某字段A表有100条记录,B表有1000条记录,A.c1字段和B.c2字段均有值。我要将A表100
将A表某字段值随机插入B表某字段
A表有100条记录,B表有1000条记录,A.c1字段和B.c2字段均有值。
我要将A表100条记录的A.c1字段值随机插入B表1000条记录的B.c2字段,求SQL语句或存储过程!
[解决办法]
- SQL code
declare @A table (c1 int,c2 varchar(1))insert into @Aselect 1,'a' union allselect 2,'b' union allselect 3,'c' union allselect 4,'d'declare @B table (c1 int,c2 varchar(1))insert into @Bselect 1,'a' union allselect 2,'b' union allselect 3,'c' union allselect 4,'d' union allselect 5,'e' union allselect 6,'f' union allselect 7,'g' union allselect 8,'h' union allselect 9,'i' union allselect 10,'j'declare @i int set @i=0update @B set c2=(select top 1 c1 from @A order by newid()),@i=@i+1select * from @B/*c1 c2----------- ----1 42 23 34 15 46 27 48 29 110 2*/
