想把某个数据库中表的一部分数据导入到另一个结构相同的数据库表中,但倒过来的数据插入时顺序要随机,即不和原来的表相同。请问应该怎么写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()

解决方案 »

  1.   

    if object_id('tb') is not null  drop table tb 
    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 行受影响)
      

  2.   


    insert into   数据库名b..表名(字段名) select 字段 from  数据库名a..表名 order by neweid()
      

  3.   

    insert into   数据库名b..表名(字段名) select 字段 from  数据库名a..表名 order by newid()
      

  4.   

    set rowcount 1000insert into tabA
    select * from tabB
    where not exists (select 1 from tabA where tabA.唯一字段=tabB.唯一字段)
    order by newid()set rowcount 0
      

  5.   

    从数据本身的角度来看确实意义不大,但从我们网站的需求角度来看,很有意义。
    现在表中的数据有一个问题是“tabA.唯一字段=tabB.唯一字段”实际是不存在的,因为拷贝过来的数据会经过编辑,但主要内容不动。
      

  6.   

    用order by newid()就可以取出随机的数据了,再插入
      

  7.   

    抱歉,可能是我说的不够清楚,我的目的是要:首页,从源表中按顺序调取数据,比如我根据规则取出了其中1000条,但这些数据不一定都是连续的;然后,把这1000条数据打乱原有顺序,按随机顺序(order by newID())重新插入到新表中。我修改下我的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 article_id asc