如何把表里的数据生成批量insert语句?
就是数据库表中的一笔就生成一个insert ?

解决方案 »

  1.   

    declare @s varchar(8000)
    set @s=''
    select @s=@s+' insert into newtable select '+[编号]+','+[数目]+ from tb 
    print @s
      

  2.   

    http://hi.baidu.com/trip008/blog/item/6f6fb1259c68916434a80f8b.html
      

  3.   

    select
    'insert into temp(.....)select......  from table
    '
    from table
      

  4.   

    我对二楼和四楼的做了一下综合,得下下面的语句:declare @s varchar(8000)
    set @s=''
    select @s=@s+' insert into sys_menu values( '+ convert(varchar(4),[id]) + ','+[name]  + ');'  from sys_menu
    print @s不知道是不是你们的意思?(此方法如何得到换行的结果?而不用分号? )
    如果不行只能用游标,循环列循环行了... 
      

  5.   

    用SQL 脚本来做。比如动态语句。
    declare @s varchar(8000)
    set @s=''
    select @s=@s+' insert into newtable select '+[编号]+','+[数目]+ from tb 
    print @s这个方法就不错
      

  6.   

    共享一下:这个存储过程可以做到:而且很强大
    create proc proc_insert (@tablename varchar(256)) 
    as 
    begin 
    set nocount on 
    declare @sqlstr varchar(4000) 
    declare @sqlstr1 varchar(4000) 
    declare @sqlstr2 varchar(4000)
    select 'SET IDENTITY_INSERT '+@tablename +' ON' 
    select @sqlstr='select ''insert '+@tablename 
    select @sqlstr1='' 
    select @sqlstr2=' (' 
    select @sqlstr1= ' values ( ''+' 
    select @sqlstr1=@sqlstr1+col+'+'',''+' ,@sqlstr2=@sqlstr2+name +',' from (select case 
    -- when a.xtype =173 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.length*2+2)+'),'+a.name +')'+' end' 
    when a.xtype =104 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(1),'+a.name +')'+' end' 
    when a.xtype =175 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end' 
    when a.xtype =61 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'convert(varchar(23),'+a.name +',121)'+ '+'''''''''+' end' 
    when a.xtype =106 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.xprec+2)+'),'+a.name +')'+' end' 
    when a.xtype =62 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(23),'+a.name +',2)'+' end' 
    when a.xtype =56 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(11),'+a.name +')'+' end' 
    when a.xtype =60 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(22),'+a.name +')'+' end' 
    when a.xtype =239 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end' 
    when a.xtype =108 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.xprec+2)+'),'+a.name +')'+' end' 
    when a.xtype =231 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end' 
    when a.xtype =59 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(23),'+a.name +',2)'+' end' 
    when a.xtype =58 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'convert(varchar(23),'+a.name +',121)'+ '+'''''''''+' end' 
    when a.xtype =52 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(12),'+a.name +')'+' end' 
    when a.xtype =122 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(22),'+a.name +')'+' end' 
    when a.xtype =48 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(6),'+a.name +')'+' end' 
    -- when a.xtype =165 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.length*2+2)+'),'+a.name +')'+' end' 
    when a.xtype =167 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end' 
    else '''NULL''' 
    end as col,a.colid,a.name 
    from syscolumns a where a.id = object_id(@tablename) and a.xtype <>189 and a.xtype <>34 and a.xtype <>35 and a.xtype <>36 
    )t order by colid select @sqlstr=@sqlstr+left(@sqlstr2,len(@sqlstr2)-1)+') '+left(@sqlstr1,len(@sqlstr1)-3)+')'' from '+@tablename 
    -- print @sqlstr 
    exec( @sqlstr) 
    select 'SET IDENTITY_INSERT '+@tablename +' OFF'
    set nocount off 
    end 
    go 
      

  7.   

    我用上面我的存储过和加上osql 
    但是为什么产生的文件里好多'------------------------------'和空格呀? 
    乍么去屏掉呀?
    osql  -S 127.0.0.1 -d data_base1  -U sa -P pass -Q  "gen_insert 'sys_user'" >> ".\sys_user.sql" 
    下面这句同样有这样的问题 
    osql  -S 127.0.0.1 -d data_base1 -U sa -P pass -Q "select * from sys_menu " -o ".\sys_menu.sql" 
    在线等待,谢谢!
      

  8.   

    我用上面我的存储过和加上osql 
    但是为什么产生的文件里好多'------------------------------'和空格呀?  
    乍么去屏掉呀? 
    osql  -S 127.0.0.1 -d data_base1  -U sa -P pass -Q  "gen_insert 'sys_user'" >> ".\sys_user.sql" 
    下面这句同样有这样的问题  
    osql  -S 127.0.0.1 -d data_base1 -U sa -P pass -Q "select * from sys_menu " -o ".\sys_menu.sql" 
    在线等待,谢谢!
      

  9.   

    ??
    不明白? 可否讲明白点? 对每一个表产生insert 语句已做好我只是想批量把所有的表导到一个SQL文件中去
    (如11楼:用osql出现好多'------------------------------'和空格呀?)