少量数据直接导出成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脚本文件,而且还添加了事务处理代码,
看来考虑得非常周到。