我用的是Sql 2008,我想把某一个表(不是数据库中的所有表)连同表数据生成一个脚本。
【右键点击表】 --> 【编写表脚本为】 --> 【CREATE到】 --> 【新查询编辑器窗口】,但得到的是数据表的结构脚本,无法得到连同数据的脚本。
看在结贴率的份上,愿出50分指教,谢谢了!

解决方案 »

  1.   

    第一,bcp 可以导出数据,然后再用bcp导入
    第二,自动生成的表结构脚本,可能无法使用bcp导入
         举个例子,你最初创建表时候,是B,C,X,D,E,A
                 那么你自动生成表结构标准后,可能会这样 A,B,C,D,E,X 导致 无法识别数据
                  以上字母为字段的替代
      

  2.   

    2008可以的,在【选择脚本项】时,把编写数据脚本 设置为:true就可以了。
      

  3.   

    其实我的目的是, 把一个表完全复制(连同表结构和表数据)到另一个表中的最简单的方法,这边一创建脚本,那边一执行,就创建了相同的表。
    不用insert命令,这个太麻烦,因为设计到数据库连接。
      

  4.   

    如果要经常做这样的操作的话,就用Toad for SQL Server 4.6 Trial吧!
    我就用这个,因为技术不咋的,不敢跨服务器更新数据
      

  5.   

    既然数据库生成脚本可以带数据生成,为什么表生成就不能带数据生成,哎!!!
    看来还得期待sql更高的版本了。
      

  6.   


    ALTER proc [dbo].[sp_CreateInsertScript] (@tablename varchar(256),@con nvarchar(400)) 
    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 =127 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(6),'+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 + ' where 1=1 and ' + isnull(@con,'') 
    print @sqlstr 
    exec( @sqlstr) 
    set nocount off 
    end
      

  7.   

    黄哥的我试过了,可以的,原帖的地址在这http://bbs.csdn.net/topics/390282055,楼主可以自己做个测试表试试
      

  8.   

    第一个参数:表名,
    第二个参数:条件名,如果整表导出来就传''
    例子:
     exec [sp_CreateInsertScript] 'tbname','dictypeid = 61' 
      

  9.   

    CREATE PROCEDURE dbo.UspOutputData 
    @tablename sysname 
    AS 
    declare @column varchar(1000) 
    declare @columndata varchar(1000) 
    declare @sql varchar(4000) 
    declare @xtype tinyint 
    declare @name sysname 
    declare @objectId int 
    declare @objectname sysname 
    declare @ident int set nocount on 
    set @objectId=object_id(@tablename) if @objectId is null -- 判断对象是否存在 
    begin 
    print 'The object not exists' 
    return 
    end 
    set @objectname=rtrim(object_name(@objectId)) if @objectname is null or charindex(@objectname,@tablename)=0
    begin 
    print 'object not in current database' 
    return 
    end if OBJECTPROPERTY(@objectId,'IsTable') < > 1 -- 判断对象是否是table 
    begin 
    print 'The object is not table' 
    return 
    end select @ident=status&0x80 from syscolumns where id=@objectid and status&0x80=0x80 if @ident is not null 
    print 'SET IDENTITY_INSERT '+@TableName+' ON' declare syscolumns_cursor cursor for select c.name,c.xtype from syscolumns c where c.id=@objectid order by c.colid open syscolumns_cursor 
    set @column='' 
    set @columndata='' 
    fetch next from syscolumns_cursor into @name,@xtype while @@fetch_status < >-1 
    begin 
    if @@fetch_status < >-2 
    begin 
    if @xtype not in(189,34,35,99,98) --timestamp不需处理,image,text,ntext,sql_variant 暂时不处理 begin 
    set @column=@column+case when len(@column)=0 then'' else ','end+@name set @columndata=@columndata+case when len(@columndata)=0 then '' else ','','',' 
    end +case when @xtype in(167,175) then '''''''''+'+@name+'+''''''''' --varchar,char 
    when @xtype in(231,239) then '''N''''''+'+@name+'+''''''''' --nvarchar,nchar 
    when @xtype=61 then '''''''''+convert(char(23),'+@name+',121)+''''''''' --datetime 
    when @xtype=58 then '''''''''+convert(char(16),'+@name+',120)+''''''''' --smalldatetime 
    when @xtype=36 then '''''''''+convert(char(36),'+@name+')+''''''''' --uniqueidentifier 
    else @name end end end fetch next from syscolumns_cursor into @name,@xtype end close syscolumns_cursor 
    deallocate syscolumns_cursor set @sql='set nocount on select ''insert '+@tablename+'('+@column+') values(''as ''--'','+@columndata+','')'' from '+@tablename print '--'+@sql 
    exec(@sql) if @ident is not null 
    print 'SET IDENTITY_INSERT '+@TableName+' OFF' GO exec UspOutputData 你的表名或者用动软代码生成器