您的位置:首页 > 数据库

根据数据生成 INSERT INTO ... 的 SQL (.Net C#, T-SQL Store Procedure 分别实现)

2010-11-16 11:07 621 查看
/*
今天看了 昨天 灵感的 随笔: http://unruledboy.cnblogs.com/archive/2005/07/18/SmartDBScripts.html
类似的工具也曾写过,今天拿出改 改,重新分享给大家!

1. .Net 1.1 C# 实现
这 仅仅是一个命令行工具,根据指定的 SQL Select 查询:
select * from Table
来生成 insert into ... 的 SQL

生成的 SQL 的 列之间使用 "\t" Tab 分割,
这样生成的 SQL, Copy & Paste 到 Excel 中,利于编辑,很轻松就可以删掉不想要的列

另外还可以控制是否处理 二进制 的大字段!

实现方式使用 DataSet 和 DataReader 两种方法重载!
区别 是:

DataReader.GetDataTypeName(i) 方法可以得到 native 的 SQL Server 的数据类型

DataReader.GetFieldType(i) 方法
DataSet 的 DataTable 的 DataColumn.DataType 属性

都只能得到 .Net 的数据类型!
完整程序下载
http://files.cnblogs.com/Microshaoft/SqlGen.cs.rar

2. T-SQL Store Procedure 实现
有局限性,数据量太大无法生成完整正确的 insert into .... SQL!
*/

1. .Net C#

namespace Microshaoft.ApplicationTest

//================================================================================================================

namespace Microshaoft.Util

//================================================================================================================

// 下面是 Microsoft SqlHelper :

//Data Access Application Block 3.1
// http://www.gotdotnet.com/workspaces/workspace.aspx?id=c20d12b0-af52-402b-9b7c-aaeb21d1f431 // SqlHelper.v3.1.cs
//csc.exe SqlHelper.v3.1.cs /t:library /r:C:\WINNT\Microsoft.NET\Framework\v1.1.4322\System.Data.OracleClient.dll

2. T-SQL Store Procedure 实现
有局限性,如果字段太多无法生成完整 正确的 Select 'insert into ....' 的 SQL!

create proc Z_SP_GenInsertSQL (@tablename varchar(256))
as
begin
declare @sql varchar(8000)
declare @sqlValues varchar(8000)
set @sql =' (' + char(9)
set @sqlValues = 'values '+ char(9) + '(' + char(9) + '''+'
select @sqlValues = @sqlValues + cols + ' + '',' + char(9) + ''' + ' ,@sql = @sql + '[' + name + '],' + CHAR(9)
from
(select case
when xtype in (48,52,56,59,60,62,104,106,108,122,127)
then 'case when '+ name +' is null then ''NULL'' else ' + 'cast('+ name + ' as varchar)'+' end'
when xtype in (58,61)
then 'case when '+ name +' is null then ''NULL'' else '+''''''''' + ' + 'cast('+ name +' as varchar)'+ '+'''''''''+' end'
when xtype in (167,175)
then 'case when '+ name +' is null then ''NULL'' else '+''''''''' + ' + 'replace('+ name+','''''''','''''''''''')' + '+''''''''' + ' end'
when xtype in (231,239)
then 'case when '+ name +' is null then ''NULL'' else '+'''N'''''' + ' + 'replace('+ name+','''''''','''''''''''')' + '+''''''''' + ' end'
else '''NULL'''
end as Cols,name
from syscolumns
where id = object_id(@tablename) and autoval is null
) T
set @sql ='select ''INSERT INTO ' + CHAR(9) + '['+ @tablename + ']' + CHAR(9) + left(@sql,len(@sql)-2) + char(9) + ') ' + CHAR(9) + left(@sqlValues,len(@sqlValues)-5) + char(9) + ')'' from '+@tablename
print @sql
exec (@sql)
end

2005-08-02 T-SQL Store Procedure 修订为:
完全不受字段数量或字段值影响 而生成正确完整的 INSERT INTO ... SQL

ALTER procedure Z_SP_GenInsertSQL
(
@tablename varchar(256)
,@WhereClause varchar(1000) = 'where 1 = 1'
)
as
begin
declare @sql varchar(8000)
declare @sqlValues varchar(8000)
set @sql =' ''(''' + char(13) + ','
set @sqlValues = ' values ('''+ char(13) + ','
select @sqlValues = @sqlValues + cols + ' + '',' + '''' + char(13) + ','
,@sql = @sql + '''[' + name + '],''' + char(13) + ','
from
(
select
case
when xtype in (48,52,56,59,60,62,104,106,108,122,127)
then 'case when '+ name + ' is null then ''NULL'' else ' + 'cast(' + name + ' as varchar)' + ' end'
when xtype in (58,61)
then 'case when '+ name + ' is null then ''NULL'' else ' + ''''''''' + ' + 'cast(' + name + ' as varchar)' + '+''''''''' + ' end'
when xtype in (167,175)
then 'case when '+ name + ' is null then ''NULL'' else ' + ''''''''' + ' + 'replace(' + name + ','''''''','''''''''''')' + ' + ''''''''' + ' end'
when xtype in (231,239)
then 'case when '+ name + ' is null then ''NULL'' else ' + '''N'''''' + ' + 'replace(' + name + ','''''''','''''''''''')' + ' + ''''''''' + ' end'
else '''NULL'''
end as Cols
,name
from syscolumns
where id = object_id(@tablename)
--and autoval is null -- 忽略自增整型字段
) T
set @sql = 'select ' + char(13) + '''INSERT INTO '''+ char(13) + ','
+ '''['+ @tablename + ']''' + char(13) + ','
+ left(@sql,len(@sql)-4) + '''' + char(13) + ','')' + left(@sqlValues,len(@sqlValues)-7) + ','')'''
+ char(13) + 'from [' + @tablename + ']'
+ char(13) + @WhereClause
--select @sql -- select SQL 被截断
print @sql -- print SQL 是完整正确的
exec (@sql)
end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: