天天看點

MS SQL Server将資料導出Insert語句的幾種方案

少量資料直接導出成SQL語句,使用起來比較友善。大量資料,就用SQL Server的導入導出功能了。

使用過幾種方式(仍然是用Google去搜尋……):

1.直接使用存儲過程

SQL Server 2000使用存儲過程導出insert語句

今天使用了一個通過存儲過程生成sqlserver資料庫insert語句的存儲過程,我覺得很好用,謝謝這位高手了

if exists (select * from sysobjects where type='p' and  name='UspOutputData')  

   drop proc UspOutputData  

GO  

CREATE PROCEDURE dbo.UspOutputData   

@tablename sysname   

AS   

declare @column varchar(2000)   

declare @columndata varchar(2000)   

declare @sql varchar(8000)   

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   

-- 判斷對象是否是table   

if OBJECTPROPERTY(@objectId,'IsTable') < > 1   

begin   

print 'The object is not table'   

return   

end   

--不知道列印的意義  

select @ident=status&0x80 from syscolumns where [email protected] and status&0x80=0x80   

if @ident is not null   

print 'SET IDENTITY_INSERT '[email protected]+' ON'   

declare syscolumns_cursor cursor  

for select c.name,c.xtype from syscolumns c where [email protected] 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 @colum[email protected]+case when len(@column)=0 then'' else ','[email protected]    

            set @[email protected]+case when len(@columndata)=0 then '' else ','','','end   

                +case when @xtype in(167,175) then '''''''''+'[email protected]+'+''''''''' --varchar,char   

                     when @xtype in(231,239) then '''N''''''+'[email protected]+'+''''''''' --nvarchar,nchar   

                     when @xtype=61 then '''''''''+convert(char(23),'[email protected]+',121)+''''''''' --datetime   

                      when @xtype=58 then '''''''''+convert(char(16),'[email protected]+',120)+''''''''' --smalldatetime   

                     when @xtype=36 then '''''''''+convert(char(36),'[email protected]+')+''''''''' --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 INTO '[email protected]+'('[email protected]+') values(''as ''--'','[email protected]+','');'' from '[email protected]   

print '--'[email protected]   

exec(@sql)   

--不知道列印的意義  

if @ident is not null   

print 'SET IDENTITY_INSERT '[email protected]+' OFF'   

GO  

exec UspOutputData t_xt_position_level 

覺得這個不錯,最後使用exec UspOutputData T_Sys_Dict_Data 執行存儲過程就ok了。 T_Sys_Dict_Data 是表名,任何表都可以。謝謝!

這種方式看起來比較簡單,而且不用安裝軟體。不過作者沒有寫支援text/ntext的代碼。

2.使用Codematic代碼生成工具

使用動軟的Codematic代碼生成工具,裡面有生成SQL方面的功能

http://www.maticsoft.com/

codematic是免費軟體,好處是成批生成的,最後生成一個檔案,但使用中發現有兩個BUG:

A.如果内容中有單引号,不會被轉換為兩個單引号,是以可能出錯(.NET1.1版有2.0版都有這個問題)

B.如果資料中有為空字元的内容(估計為NULL的也會),就會把這一列省略掉生成在INSERT語句中,

這個看似智能的功能,在某例本來不允許為NULL,但填寫的空字元就出錯了。

3.使用MyGeneration

Create Tables and Build inserts from Tables by using Mygeneration Templates(Sql Server)()

http://hardrock.cnblogs.com/archive/2005/12/16/298386.html

MyGeneration是免費軟體,我用的是1.3,據說功能強大,不過我沒怎麼用得上。

速度非常快,而且生成的代碼也沒有Codematic裡提到的問題,生成結果是每個表一個SQL腳本檔案,而且還添加了事務處理代碼,

看來考慮得非常周到。