想把某个数据库中表的一部分数据导入到另一个结构相同的数据库表中,但倒过来的数据插入时顺序要随机,即不和原来的表相同。请问应该怎么写SQL语句?我现在的SQL语句只能实现插入的数据顺序和原表相同。
insert into [B].[dbo].[article](channel,class1,class2,title,subtitle,keywords,description,content,tags,time,image,zhiding,tuijian,hits,adduser,status)
select channel,class1,class2,title,subtitle,keywords,description,content,null,time,null,0,0,hits,adduser,0 from [A].[dbo].[article] where channel=1 and class1=1 order by newID()
insert into [B].[dbo].[article](channel,class1,class2,title,subtitle,keywords,description,content,tags,time,image,zhiding,tuijian,hits,adduser,status)
select channel,class1,class2,title,subtitle,keywords,description,content,null,time,null,0,0,hits,adduser,0 from [A].[dbo].[article] where channel=1 and class1=1 order by newID()
go
create table tb(sID int,s varchar(100))
insert [tb]
select 1,'13.27~13.50~13.74' union all
select 2,'7.25~7.38' union all
select 3,'10.81~11~11.20' union all
select 4,'37.775' union all
select 5,'100~文字' union all
select 6,'abc' union all
select 7, ' ' union all
select 8,'9.90~10.08~10.25~10.35'
if object_id('ta') is not null drop table ta
go
create table ta(sID int,s varchar(100))
------------------------------------------------------------------
insert into ta select * from tb order by newid() ----这个语句应该可以的select * from tasID s
----------- ----------------------------------------------------------------------------------------------------
8 9.90~10.08~10.25~10.35
5 100~文字
1 13.27~13.50~13.74
7
3 10.81~11~11.20
4 37.775
6 abc
2 7.25~7.38(8 行受影响)
insert into 数据库名b..表名(字段名) select 字段 from 数据库名a..表名 order by neweid()
select * from tabB
where not exists (select 1 from tabA where tabA.唯一字段=tabB.唯一字段)
order by newid()set rowcount 0
现在表中的数据有一个问题是“tabA.唯一字段=tabB.唯一字段”实际是不存在的,因为拷贝过来的数据会经过编辑,但主要内容不动。
insert into [B].[dbo].[article](channel,class1,class2,title,subtitle,keywords,description,content,tags,time,image,zhiding,tuijian,hits,adduser,status)
select channel,class1,class2,title,subtitle,keywords,description,content,null,time,null,0,0,hits,adduser,0 from [A].[dbo].[article] where channel=1 and class1=1 order by article_id asc