产生insert脚本[转]
思韵闪耀
2012-08-14
0
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 (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(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 '+@tablename
      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


【版权声明】
本站部分内容来源于互联网,本站不拥有所有权,不承担相关法律责任。如果发现本站有侵权的内容,欢迎发送邮件至masing@13sy.com 举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。

RIP

上一篇: 跟踪状态

下一篇: RAID configurations

相关内容

网页保护、网页图片保护
1、禁止另存网页,把如下代码加入到网页body/body中 程序代...
2025-05-30
docker-compos...
1.Compose介绍 DockerCompose是一个用来定义和...
2024-04-26
firewall-cmd ...
firewalld的简要说明:firewalld 、firewal...
2024-03-15
DNF error: Er...
U盘引导最小化安装anolis oa 8.8 ga出现以下错误DN...
2023-10-27
列出服务单元: syste...
列出服务单元: systemctl list-units --ty...
2022-11-26
续订Exchange Se...
每个证书都有内置的到期日期。 在Exchange Server中,...
2022-09-16

热门资讯

sql中int型与varcha... sql中int转varchar或nvarchar,varchar或nvarchar转int的方法: ...
SQLSERVERAGENT ... 上的 SQLSERVERAGENT 服务启动过,然后又停止了。 (ObjectExplorer) 可...
SQL Server 中4个系... SQL Server 中4个系统数据库,Master、Model、Msdb、Tempdb 系统数据库...
SQL Server中如何设置... 对于已经建好的数据库表,是不能在SQL Server Management中可视化地修改ID为自增长...
该表已为了复制而被发布,所以... 场景:从发布库上将一数据库移到另一服务器,在对表改名时提示该表已为了复制而被发布,所以无法重命名。 ...
SQL Server 2008... SQL Server 2008 R2运行越久,占用内存会越来越大。 第一种: 有了上边的分析结果,解...
SQL Server (MSS... SQL Server (MSSQLSERVER) 启动后 自动生成文件 audittrace2022...
如果使用没有提供选项值的 Sq... 如果使用没有提供选项值的 SqlDependency,必须先调用 SqlDependency.Sta...
传递给数据库 'master'... 传递给数据库 master 中的日志扫描操作的日志扫描号无效 错误:连接数据库的时候提示:SQL S...
数据仓库SSAS+SSIS+... 数据仓库SSAS+SSIS+SSRS SSAS- 1,用ssas生成多维度,然后利用excel的da...