MSSQL中把表中的数据导出成Insert

use master
go
if exists (select name from sysobjects where name = ‘sp_generate_insert_script‘)
begin
  drop proc sp_generate_insert_script
  print ‘old version of sp_generate_insert_script dropped‘
end
go
create procedure sp_generate_insert_script
                 @tablename_mask varchar(30) = NULL
as
begin
  declare @tablename       varchar (128)
  declare @tablename_max   varchar (128)
  declare @tableid         int
  declare @columncount     numeric (7,0)
  declare @columncount_max numeric (7,0)
  declare @columnname      varchar (30)
  declare @columntype      int
  declare @string          varchar (30)
  declare @leftpart        varchar (8000)
  declare @rightpart       varchar (8000)
  declare @hasident        int
  set nocount on
  -- take ALL tables when no mask is given (!)
  if (@tablename_mask is NULL)
  begin
    select @tablename_mask = ‘%‘
  end
  -- create table columninfo now, because it will be used several times
  create table #columninfo
  (num      numeric (7,0) identity,
   name     varchar(30),
   usertype smallint)

  select name,
         id
    into #tablenames
    from sysobjects
   where type in (‘U‘ ,‘S‘)
     and name like @tablename_mask
  -- loop through the table #tablenames
  select @tablename_max  = MAX (name),
         @tablename      = MIN (name)
    from #tablenames
  while @tablename <= @tablename_max
  begin
    select @tableid   = id
      from #tablenames
     where name = @tablename
    if (@@rowcount <> 0)
    begin
      -- Find out whether the table contains an identity column
      select @hasident = max( status & 0x80 )
        from syscolumns
       where id = @tableid
      truncate table #columninfo
      insert into #columninfo (name,usertype)
      select name, type
        from syscolumns C
       where id = @tableid
         and type <> 37            -- do not include timestamps
      -- Fill @leftpart with the first part of the desired insert-statement, with the fieldnames
      select @leftpart = ‘select ‘‘insert into ‘[email protected]
      select @leftpart = @leftpart + ‘(‘
      select @columncount     = MIN (num),
             @columncount_max = MAX (num)
        from #columninfo
      while @columncount <= @columncount_max
      begin
        select @columnname = name,
               @columntype = usertype
          from #columninfo
         where num = @columncount
        if (@@rowcount <> 0)
        begin
          if (@columncount < @columncount_max)
          begin
            select @leftpart = @leftpart + @columnname + ‘,‘
          end
          else
          begin
            select @leftpart = @leftpart + @columnname + ‘)‘
          end
        end
        select @columncount = @columncount + 1
      end
      select @leftpart = @leftpart + ‘ values(‘‘‘
      -- Now fill @rightpart with the statement to retrieve the values of the fields, correctly formatted
      select @columncount     = MIN (num),
             @columncount_max = MAX (num)
        from #columninfo
      select @rightpart = ‘‘
      while @columncount <= @columncount_max
      begin
        select @columnname = name,
               @columntype = usertype
          from #columninfo
         where num = @columncount
        if (@@rowcount <> 0)
        begin
          if @columntype in (39,47)
          begin
            select @rightpart = @rightpart + ‘+‘
            select @rightpart = @rightpart + ‘ISNULL(‘ + replicate( char(39), 4 ) + ‘+replace(‘ + @columnname + ‘,‘ +
replicate( char(39), 4 ) + ‘,‘ + replicate( char(39), 6) + ‘)+‘ + replicate( char(39), 4 ) + ‘,‘‘NULL‘‘)‘
          end
          else if @columntype = 35

          begin
            select @rightpart = @rightpart + ‘+‘
            select @rightpart = @rightpart + ‘ISNULL(‘ + replicate( char(39), 4 ) + ‘+replace(convert(varchar(1000),‘ +
@columnname + ‘)‘ + ‘,‘ + replicate( char(39), 4 ) + ‘,‘ + replicate( char(39), 6 ) + ‘)+‘ + replicate( char(39), 4 ) +
‘,‘‘NULL‘‘)‘
          end
          else if @columntype in (58,61,111)
          begin
            select @rightpart = @rightpart + ‘+‘
            select @rightpart = @rightpart + ‘ISNULL(‘ + replicate( char(39), 4 ) + ‘+convert(varchar(20),‘ + @columnname +
‘)+‘+ replicate( char(39), 4 ) + ‘,‘‘NULL‘‘)‘
          end
          else
          begin
            select @rightpart = @rightpart + ‘+‘
            select @rightpart = @rightpart + ‘ISNULL(convert(varchar(99),‘ + @columnname + ‘),‘‘NULL‘‘)‘
          end

          if ( @columncount < @columncount_max)
          begin
            select @rightpart = @rightpart + ‘+‘‘,‘‘‘
          end
        end
        select @columncount = @columncount + 1
      end
    end
    select @rightpart = @rightpart + ‘+‘‘)‘‘‘ + ‘ from ‘ + @tablename
    -- Order the select-statements by the first column so you have the same order for
    -- different database (easy for comparisons between databases with different creation orders)
    select @rightpart = @rightpart + ‘ order by 1‘
    -- For tables which contain an identity column we turn identity_insert on
    -- so we get exactly the same content
    if @hasident > 0
       select ‘SET IDENTITY_INSERT ‘ + @tablename + ‘ ON‘
    exec ( @leftpart + @rightpart )
    if @hasident > 0
       select ‘SET IDENTITY_INSERT ‘ + @tablename + ‘ OFF‘
    select @tablename      = MIN (name)
      from #tablenames
     where name            > @tablename
  end
end

再选择要导出语句的数据库,在“查询”中选择“结果保存为文件……”,执行EXEC sp_generate_insert_script ‘表名‘,如果不写表名,将

导出数据库中所有的表的内容。保存的文件即为Insert的SQL语句。

时间: 2024-08-14 10:21:29

MSSQL中把表中的数据导出成Insert的相关文章

在Action中获取表单提交数据

-----------------siwuxie095 在 Action 中获取表单提交数据 1.之前的 Web 阶段是提交表单到 Servlet,在其中使用 Request 对象 的方法获取数据 2.Struts2 是提交表单到 Action,但 Action 没有 Request 对象,不能 直接使用 Request 对象获取数据 「可以间接使用 Request 对象获取数据」 3.Action 获取表单提交数据主要有三种方式: (1)使用 ActionContext 类 (2)使用 Ser

查看hive中某个表中的数据、表结构及所在路径

查看hive中action_data_myisam表中的数据.表结构及所在路径 1.客户端进入hive环境:hive 2.查看表数据,鉴于数据量大,这里只显示前五条:select * from action_data_myisam limit 5; 3.查看表结构:desc action_data_myisam; 4.查看此表所在路径:describe extended action_data_myisam; 图1针对1.2.3步 图2 针对第4步 end!

SAP IDES中SPFLI表中生成数据

安装好IDES后,发现SPFLI表中竟然没有数据,百度了一下,网友给出一段代码,运行以后,SPFLI中就有数据了,代码如下: *&---------------------------------------------------------------------* *& Report ZLIU_008 *& www.qiqubaike.com *&-------------------------------------------------------------

快速删除数据库中所有表中的数据

原文:快速删除数据库中所有表中的数据 select 'truncate table ' + Name + ';' from sysobjects where xtype='U' order by name asc; 该条语句执行之后会将数据库中所有的表都查询出来,复制出来之后执行truncate语句即可 sysobjects 在数据库内创建的每个对象(约束.默认值.日志.规则.存储过程等)在表中占一行.只有在 tempdb 内,每个临时对象才在该表中占一行. 列名 数据类型 描述 name sy

在sqlServer中把数据导出为insert脚本

有时候为了把数据导出为insert脚本,不得不用一些小工具,或者通过自己写存储过程来完成这一操作.其实SqlServer本身就有这种功能.以下是详细步骤:

ORACLE中查看表中的外键来源于哪些表

1. ORACLE中查看表中的外键来源于哪些表 select cl.table_name from user_cons_columns cl left join user_constraints c on cl.constraint_name = c.r_constraint_name where c.constraint_type = 'R' and c.table_name = '表名' 2.Oracle中查看表中的主键被被哪些表引用为外键

如何将一个数据库中的一个表复制到另一个数据库中的表中

如何将一个数据库中的一个表复制到另一个数据库中的表中 2013-09-11 17:13匿名 | 浏览 13763 次 如何将一个数据库中的一个表复制到另一个数据库中的表中两个表的列名不同.有知道的么?SQL语句直接导过去.! 2013-09-12 20:26网友采纳 热心网友 如果另一个库中没有同名的表select * into b数据库.dbo.a表 from a数据库.dbo.a表 where 条件 如果是追加到另一个表中inert into b数据库.dbo.a表select * from

C# 泛型List对象数据 导出 成 EXCEL数据表

1          /// <summary>  2         /// 将一组对象导出成EXCEL  3         /// </summary>  4         /// <typeparam name="T">要导出对象的类型</typeparam>  5         /// <param name="objList">一组对象</param>  6         //

expdp导出oracle数据库中指定表空间下数据

大家在工作中,应该很多接触各种各样的数据库,笔者在此记录一下,我的一次导出数据经历. 工作环境是oracle+Linux(红帽系统,类似centos) 1.进入linux系统(这里不在赘余) 2.运行sqlplus,建议创建oracle用户 管理数据库,直接输入sqlplus 可能不行,需要修改系统的环境变量,我这里是用的 . /home/oracle/.bash_profile 临时改变系统环境变量 3.在进入sqlplus 中,创建目录路径:输入命令: create directory my