我生成的sql脚本文件为什么没有数据。
比如说有个table t1
有两个字段 a  b
          1  2
          3  5我下面每个字段有数据的,
但生成的sql脚本里他只是把表和字段名获取生成。
而没有把数据生成到sql脚本。有经验 的兄弟帮个忙,谢谢

解决方案 »

  1.   


    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 @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)  
      set nocount off  
     end   
    go
    if object_id('tb') is not null
    drop table tb
    go
    create table tb(id int,name varchar(50))
    insert into tb select 1,'abc'
    insert into tb select 2,'abc'
    insert into tb select 3,'abc'
    insert into tb select 4,'abc'exec dbo.proc_insert 'tb'insert tb (id,name)  values ( 1,'abc')
    insert tb (id,name)  values ( 2,'abc')
    insert tb (id,name)  values ( 3,'abc')
    insert tb (id,name)  values ( 4,'abc')
      

  2.   

    楼上什么意思啊??兄弟,我的意思就是说从企业管理器,然后再对应的表
    右击,所有任务,生成sql脚本。。然后是不是还有其他设置,让他把数据也生成到sql脚本里?有经验的兄弟快帮个忙啊
      

  3.   

    我知道oracle可以,但是sql server好像不带这个功能吧
      

  4.   

    SQL SERVER自身的生成脚本是不具备这个功能的,你可以在网上下个工具试一下!
      

  5.   

    --楼主的建表脚本已经有了,下面再生成个插入数据的脚本(可以临时用下,当然没有网上的导出工具好用了)
    --t1表有两字段:a int ,b varchar
    --t1表中数据:
    a       b
    -------------
    1 axt
    3 bc3
    5 axt
    6 10
    7 op
    8 ipl
    9 hjk
    10 90
    --生成插入脚本(实际使用时根据表中字段情况做具体修改)
    select 'insert into a values ('+convert(varchar,a)+','''+ b +''')' from t1--结果
    insert into a values (1,'axt')
    insert into a values (3,'bc3')
    insert into a values (5,'axt')
    insert into a values (6,'10')
    insert into a values (7,'op')
    insert into a values (8,'ipl')
    insert into a values (9,'hjk')
    insert into a values (10,'90')