码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2015-02-04 12:42:45      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:

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 ‘+@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

 

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

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

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

标签:

原文地址:http://www.cnblogs.com/webwang/p/mssqltoinsert.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!