通过脚本获取指定表的所有Insert语句

--通过脚本获取指定表的所有Insert语句。 

Create         procedure [dbo].[sp_GetInsertSQL] 
                 @tablename_mask varchar(30) = NULL 
as 
begin 
-- NOTE: If, when executing in the Query Analyzer, the result is truncated, you can remedy 
--       this by choosing Query / Current Connection Options, choosing the Advanced tab and 
--       adjusting the value of ‘Maximum characters per column‘. 
--       Unchecking ‘Print headers‘ will get rid of the line of dashes. 
 
  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 (100) 
  declare @columntype      int 
  declare @string          varchar (30) 
  declare @leftpart        varchar (max)    
  declare @rightpart       varchar (max)    
  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(100), 
   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‘ 
 
 --print @leftpart + @rightpart 
    exec ( @leftpart + @rightpart ) 
  
 
    if @hasident > 0 
       select ‘SET IDENTITY_INSERT ‘ + @tablename + ‘ OFF‘ 
 
    select @tablename      = MIN (name) 
      from #tablenames 
     where name            > @tablename 
  end 
 
end  
  
   
  
 

时间: 2024-08-05 13:59:27

通过脚本获取指定表的所有Insert语句的相关文章

获取指定表的创建脚本

--****************************************************************************-- 软件名称: May Flower Erp-- 版权所有: (C) 2005-2006 May Flower ERP 开发组-- 功能描述: 获取指定表的创建脚本,包括表和字段的属性.外键(注释掉的)----------------------------------------------------------------------

【转】MSSQL获取指定表的列名信息,描述,数据类型,长度

/* --作用:根据特定的表名查询出字段,以及描述,数据类型,长度,精度,是否自增,是否为空等信息 --作者:wonder QQ:37036846 QQ群:.NET顶级精英群 ID:124766907 --时间:2011-03-23 11:25 --描述:创建存储过程 --参数:@tableName 表名 */ CREATE PROC sp_GetListsColumnInfoByTableName( @tableName nvarchar(255)) AS BEGIN SELECT CASE

SQL Server里面导出SQL脚本(表数据的insert语句)

转载自:http://hi.baidu.com/pigarmy/blog/item/109894c445eab0a28326ac5a.html 最近需要导出一个表的数据并生成insert语句,发现SQL Server的自带工具并米有此功能.BAIDU一下得到如下方法(亲测OK) 用这个存储过程可以实现: CREATE PROCEDURE dbo.UspOutputData @tablename sysname AS declare @column varchar(1000) declare @c

MS SQLServer表数据生成Insert语句

关键词:SQLServer.表数据.生成Insert语句 反馈意见请联系:[email protected] 简介 数据库数据生成insert(MSSQL版),可将表中的数据生成insert或者update的sql脚本.比如您维护两个数据库,其中一个数据库中增加的数据也希望能够在另外一个数据中进行执行. 下载地址 下载地址:http://www.vidarsoft.cn/download/SQLInsert.zip Csdn下载地址:http://download.csdn.net/detail

使用Python脚本获取指定格式文件列表的方法

在Python环境下获取指定后缀文件列表的方式. 来源stackoverflow 这里简单以*.txt的作为例子. 使用glob(推荐) 1 import glob, os 2 os.chdir("/mydir") 3 for file in glob.glob("*.txt"): 4 print(file) 简单实用os.listdir 1 import os 2 for file in os.listdir("/mydir"): 3 if f

sql获取指定表所有列名及注释

SELECT b.name as 字段名 ,Type_name(b.xusertype) as 字段类型, Isnull(c.VALUE,'') as 字段说明FROM sysobjects a join syscolumns b on a.id = b.id JOIN sys.extended_properties c ON b.id = c.major_id AND b.colid = c.minor_id WHERE a.id = Object_id('gbpm.JLFFORM3') --

用shell脚本创建sqlite表并添加sql语句--通用

重要使用的是EOF的功能,亲测和!功能一致:下面是测试代码 #!/bin/bash val=`ls`for v in ${val} do if [ ${v} == "test.db" ] then rm test.db echo "rm test.db" fidone sqlite test.db << EOFcreate table test(name char,sex char);insert into test values("yang&

利用navicat导出数据表结构和insert语句

用了navicat一段时间了,发现navicat确实非常方便,可支持多种数据库.以前一直觉得他的到处sql文件比较麻烦,每个表都会生成一个sql文件,不方便进行导入操作.今天突然发现它也有批量到出成一个sql文件的功能. 操作步骤 菜单 : 数据传输 目标选择,然后进行相应的设置后点击开始.

SQL 从指定表筛选指定行信息 获取表行数

1.获取指定表的行数 --获取表中数据行数 --select max([列名]) from 表名 2.筛选指定表的指定行数据(数据表分页获取) http://www.cnblogs.com/morningwang/archive/2009/01/02/1367277.html