根据基本表结构及其数据生成 INSERT INTO ... 的 SQL
2008-12-13 19:44
621 查看
根据基本表结构及其数据生成 INSERT INTO ... SQL 的 T-SQL 存储过程
,在网上能够搜索出的版本主要有两个:
1. CSDN Sky_blue 所作: proc_insert (可 google)
2. CSDN playyuer 所作: spGenInsertSQL (可 google)
但这两个版本的程序都曾收录到:
http://www.cnblogs.com/kasafuma/articles/109922.html
但这两个版本程序都有局限性:
如果字段太多或字段值的内容太多而无法生成完整正确的 insert into ... 的 SQL!
例如:
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
use Northwind
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
proc_insert 'employees'
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
spGenInsertSQL 'employees'
执行后均得不到完整正确的 SQL!
其实存储过程本身的代码应该是没有错误的
,只是因为字段太多或字段值的内容太多,varchar 变量容量不够大!
,应该算 Microsoft SQL Server 的缺陷
这个问题已经存在了很久了,长达至少两年多了!
今天终于被窝想到了解决办法,其实很简单:
这两个版本程序生成的 insert sql 都只用了一个字段(变量)
只要多用几个字段即可:
原来是: select f2+f2+f3+ ... +fn
现改为: select f2,f2,f3, ... ,fn
即可!
1.新版本,降低 "字段数量或字段值内容太多" 的影响 而尽量生成正确完整的 INSERT INTO ... SQL:
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
alter procedure Z_SP_GenInsertSQL
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
(
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
@TableName varchar(256)
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
,@AllTopClause varchar(1000) = ''
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
,@WhereOrderByClause varchar(1000) = '' --'where 1 = 1 order by null'
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
)
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
as
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
begin
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
/**//*
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
usage:
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Z_SP_GenInsertSQL 'employees','all top 30 PERCENT with ties ','where [LastName] is not null order by employeeid desc'
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
declare @sql varchar(8000)
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
declare @sqlValues varchar(8000)
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
set @sql = ' ''(''' + char(13) + ','
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
set @sqlValues = ' values ('''+ char(13) + ','
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
select @sqlValues = @sqlValues + cols + ' + '',' + '''' + char(13) + ','
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
,@sql = @sql + '''[' + name + '],''' + char(13) + ','
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
from
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
(
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
select
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
case
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
when xtype in (48,52,56,59,60,62,104,106,108,122,127)
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
then 'case when ' + name + ' is null then ''NULL'' else ' + 'cast(' + name + ' as varchar)' + ' end'
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
when xtype in (58,61)
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
then 'case when ' + name + ' is null then ''NULL'' else ' + ''''''''' + ' + 'cast(' + name + ' as varchar)' + '+''''''''' + ' end'
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
when xtype in (167,175)
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
then 'case when ' + name + ' is null then ''NULL'' else ' + ''''''''' + ' + 'replace(' + name + ','''''''','''''''''''')' + ' + ''''''''' + ' end'
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
when xtype in (231,239)
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
then 'case when ' + name + ' is null then ''NULL'' else ' + '''N'''''' + ' + 'replace(' + name + ','''''''','''''''''''')' + ' + ''''''''' + ' end'
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
else '''NULL'''
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
end as Cols
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
,name
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
from syscolumns
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
where id = object_id(@TableName)
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
--and autoval is null --忽略自增整型字段
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
) T
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
set @sql = 'select ' + @AllTopClause + char(13) + '''INSERT INTO ''' + char(13) + ','
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
+ '''['+ @TableName + ']''' + char(13) + ','
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
+ left(@sql,len(@sql)-4) + '''' + char(13) + ','')' + left(@sqlValues,len(@sqlValues)-7) + ','')'''
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
+ char(13) + 'from [' + @TableName + ']'
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
+ char(13) + @WhereOrderByClause
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
--select @sql -- select SQL 被截断
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
print @sql -- print SQL 是完整正确的
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
exec (@sql)
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
/**//*
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
select *
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
from syscolumns
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
where id = object_id('test') and autoval is null
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
end
2.老版本 如果字段太多或字段值的内容太多而无法生成完整正确的 insert into ... 的 SQL
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
create proc Z_SP_GenInsertSQL (@tablename varchar(256))
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
as
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
begin
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
declare @sql varchar(8000)
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
declare @sqlValues varchar(8000)
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
set @sql =' (' + char(9)
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
set @sqlValues = 'values '+ char(9) + '(' + char(9) + '''+'
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
select @sqlValues = @sqlValues + cols + ' + '',' + char(9) + ''' + ' ,@sql = @sql + '[' + name + '],' + CHAR(9)
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
from
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
(select case
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
when xtype in (48,52,56,59,60,62,104,106,108,122,127)
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
then 'case when '+ name +' is null then ''NULL'' else ' + 'cast('+ name + ' as varchar)'+' end'
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
when xtype in (58,61)
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
then 'case when '+ name +' is null then ''NULL'' else '+''''''''' + ' + 'cast('+ name +' as varchar)'+ '+'''''''''+' end'
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
when xtype in (167,175)
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
then 'case when '+ name +' is null then ''NULL'' else '+''''''''' + ' + 'replace('+ name+','''''''','''''''''''')' + '+''''''''' + ' end'
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
when xtype in (231,239)
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
then 'case when '+ name +' is null then ''NULL'' else '+'''N'''''' + ' + 'replace('+ name+','''''''','''''''''''')' + '+''''''''' + ' end'
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
else '''NULL'''
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
end as Cols,name
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
from syscolumns
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
where id = object_id(@tablename) and autoval is null
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
) T
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
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
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
print @sql
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
exec (@sql)
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
/**//**//**//*
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
select *
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
from syscolumns
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
where id = object_id('test') and autoval is null
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
end
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
3. .Net/C# 版本请看:
http://www.cnblogs.com/Microshaoft/archive/2005/07/19/195752.html
,在网上能够搜索出的版本主要有两个:
1. CSDN Sky_blue 所作: proc_insert (可 google)
2. CSDN playyuer 所作: spGenInsertSQL (可 google)
但这两个版本的程序都曾收录到:
http://www.cnblogs.com/kasafuma/articles/109922.html
但这两个版本程序都有局限性:
如果字段太多或字段值的内容太多而无法生成完整正确的 insert into ... 的 SQL!
例如:
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
use Northwind
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
proc_insert 'employees'
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
spGenInsertSQL 'employees'
执行后均得不到完整正确的 SQL!
其实存储过程本身的代码应该是没有错误的
,只是因为字段太多或字段值的内容太多,varchar 变量容量不够大!
,应该算 Microsoft SQL Server 的缺陷
这个问题已经存在了很久了,长达至少两年多了!
今天终于被窝想到了解决办法,其实很简单:
这两个版本程序生成的 insert sql 都只用了一个字段(变量)
只要多用几个字段即可:
原来是: select f2+f2+f3+ ... +fn
现改为: select f2,f2,f3, ... ,fn
即可!
1.新版本,降低 "字段数量或字段值内容太多" 的影响 而尽量生成正确完整的 INSERT INTO ... SQL:
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
alter procedure Z_SP_GenInsertSQL
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
(
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
@TableName varchar(256)
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
,@AllTopClause varchar(1000) = ''
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
,@WhereOrderByClause varchar(1000) = '' --'where 1 = 1 order by null'
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
)
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
as
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
begin
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
/**//*
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
usage:
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Z_SP_GenInsertSQL 'employees','all top 30 PERCENT with ties ','where [LastName] is not null order by employeeid desc'
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
declare @sql varchar(8000)
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
declare @sqlValues varchar(8000)
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
set @sql = ' ''(''' + char(13) + ','
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
set @sqlValues = ' values ('''+ char(13) + ','
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
select @sqlValues = @sqlValues + cols + ' + '',' + '''' + char(13) + ','
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
,@sql = @sql + '''[' + name + '],''' + char(13) + ','
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
from
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
(
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
select
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
case
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
when xtype in (48,52,56,59,60,62,104,106,108,122,127)
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
then 'case when ' + name + ' is null then ''NULL'' else ' + 'cast(' + name + ' as varchar)' + ' end'
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
when xtype in (58,61)
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
then 'case when ' + name + ' is null then ''NULL'' else ' + ''''''''' + ' + 'cast(' + name + ' as varchar)' + '+''''''''' + ' end'
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
when xtype in (167,175)
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
then 'case when ' + name + ' is null then ''NULL'' else ' + ''''''''' + ' + 'replace(' + name + ','''''''','''''''''''')' + ' + ''''''''' + ' end'
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
when xtype in (231,239)
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
then 'case when ' + name + ' is null then ''NULL'' else ' + '''N'''''' + ' + 'replace(' + name + ','''''''','''''''''''')' + ' + ''''''''' + ' end'
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
else '''NULL'''
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
end as Cols
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
,name
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
from syscolumns
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
where id = object_id(@TableName)
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
--and autoval is null --忽略自增整型字段
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
) T
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
set @sql = 'select ' + @AllTopClause + char(13) + '''INSERT INTO ''' + char(13) + ','
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
+ '''['+ @TableName + ']''' + char(13) + ','
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
+ left(@sql,len(@sql)-4) + '''' + char(13) + ','')' + left(@sqlValues,len(@sqlValues)-7) + ','')'''
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
+ char(13) + 'from [' + @TableName + ']'
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
+ char(13) + @WhereOrderByClause
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
--select @sql -- select SQL 被截断
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
print @sql -- print SQL 是完整正确的
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
exec (@sql)
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
/**//*
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
select *
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
from syscolumns
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
where id = object_id('test') and autoval is null
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
end
2.老版本 如果字段太多或字段值的内容太多而无法生成完整正确的 insert into ... 的 SQL
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
create proc Z_SP_GenInsertSQL (@tablename varchar(256))
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
as
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
begin
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
declare @sql varchar(8000)
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
declare @sqlValues varchar(8000)
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
set @sql =' (' + char(9)
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
set @sqlValues = 'values '+ char(9) + '(' + char(9) + '''+'
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
select @sqlValues = @sqlValues + cols + ' + '',' + char(9) + ''' + ' ,@sql = @sql + '[' + name + '],' + CHAR(9)
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
from
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
(select case
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
when xtype in (48,52,56,59,60,62,104,106,108,122,127)
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
then 'case when '+ name +' is null then ''NULL'' else ' + 'cast('+ name + ' as varchar)'+' end'
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
when xtype in (58,61)
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
then 'case when '+ name +' is null then ''NULL'' else '+''''''''' + ' + 'cast('+ name +' as varchar)'+ '+'''''''''+' end'
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
when xtype in (167,175)
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
then 'case when '+ name +' is null then ''NULL'' else '+''''''''' + ' + 'replace('+ name+','''''''','''''''''''')' + '+''''''''' + ' end'
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
when xtype in (231,239)
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
then 'case when '+ name +' is null then ''NULL'' else '+'''N'''''' + ' + 'replace('+ name+','''''''','''''''''''')' + '+''''''''' + ' end'
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
else '''NULL'''
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
end as Cols,name
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
from syscolumns
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
where id = object_id(@tablename) and autoval is null
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
) T
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
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
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
print @sql
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
exec (@sql)
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
/**//**//**//*
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
select *
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
from syscolumns
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
where id = object_id('test') and autoval is null
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
end
![](http://microshaoft.cnblogs.com/Images/OutliningIndicators/None.gif)
3. .Net/C# 版本请看:
http://www.cnblogs.com/Microshaoft/archive/2005/07/19/195752.html
相关文章推荐
- T-SQL 存储过程: (修订版) 根据基本表结构及其数据生成 INSERT INTO ... 的 SQL (转)
- T-SQL 存储过程: (修订版) 根据基本表结构及其数据生成 INSERT INTO ... 的 SQL
- 根据基本表结构及其数据生成INSERT 语句的SQL脚本
- 根据基本表结构及其数据生成INSERT...的SQL
- 根据基本表结构及其数据生成 INSERT ... 的 SQL
- 根据基本表结构及其数据生成 INSERT ... 的 SQL
- 根据基本表结构及其数据生成 INSERT ... 的 SQL
- 根据基本表结构及其数据生成SQL语句
- 根据数据生成 INSERT INTO ... 的 SQL (.Net C#, T-SQL Store Procedure 分别实现)
- 根据Sql表格数据自动生成InsertInto的SP(spRD_AutoGenInsertSql)
- 根据数据生成 INSERT INTO ... 的 SQL (.Net C#, T-SQL Store Procedure 分别实现)
- (转载)根据数据字典表定义的表结构,生成创建表的SQL语句
- 如何将sqlserver表中的数据导出sql语句或生成insert into语句
- 如何将sqlserver表中的数据导出sql语句或生成insert into语句
- 如何将sqlserver表中的数据导出sql语句或生成insert into语句 [转]
- 将表中数据生成Insert into 结构的存储过程[已测试]
- 如何将sqlserver表中的数据导出sql语句或生成insert into语句
- 根据数据字典表定义的表结构,生成创建表的SQL语句
- 【转载】将sqlserver表中的数据导出sql语句或生成insert into语句