将SQLServer中表按其结构生成SQl插入语句
2006-12-26 15:28
381 查看
前两天实施需要将SQL server 中表结构生成插入语句,在网上找到解决方案,可是老出错,整理了一下,转贴在此,供参考吧!
步骤一、在sqlserver中建立存储过程。
CREATE procedure spGenInsertSQL @tablename varchar(256)
as
begin
declare @sql varchar(8000)
declare @sqlValues varchar(8000)
set @sql =' ('
set @sqlValues = 'values (''+'
select @sqlValues = @sqlValues + cols + ' + '','' + ' ,@sql = @sql + name + ','
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)
then 'case when '+ name +' is null then ''NULL'' else '+''''''''' + ' + 'replace('+ name+','''''''','''''''''''')' + '+'''''''''+' end'
when xtype in (231)
then 'case when '+ name +' is null then ''NULL'' else '+'''N'''''' + ' + 'replace('+ name+','''''''','''''''''''')' + '+'''''''''+' end'
when xtype in (175)
then 'case when '+ name +' is null then ''NULL'' else '+''''''''' + ' + 'cast(replace('+ name+','''''''','''''''''''') as Char(' + cast(length as varchar) + '))+'''''''''+' end'
when xtype in (239)
then 'case when '+ name +' is null then ''NULL'' else '+'''N'''''' + ' + 'cast(replace('+ name+','''''''','''''''''''') as Char(' + cast(length as varchar) + '))+'''''''''+' end'
else '''NULL'''
end as Cols,name
from syscolumns
where id = object_id(@tablename)
) T
set @sql ='select ''INSERT INTO '+ @tablename + left(@sql,len(@sql)-1)+') ' + left(@sqlValues,len(@sqlValues)-4) + ')'' from '+@tablename
print @sql
exec (@sql)
end
GO
步骤二、在sql查询分析器中执行:exec spGenInsertSQL 'TableName' ;
步骤三、将查询出来的结果另存。
步骤一、在sqlserver中建立存储过程。
CREATE procedure spGenInsertSQL @tablename varchar(256)
as
begin
declare @sql varchar(8000)
declare @sqlValues varchar(8000)
set @sql =' ('
set @sqlValues = 'values (''+'
select @sqlValues = @sqlValues + cols + ' + '','' + ' ,@sql = @sql + name + ','
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)
then 'case when '+ name +' is null then ''NULL'' else '+''''''''' + ' + 'replace('+ name+','''''''','''''''''''')' + '+'''''''''+' end'
when xtype in (231)
then 'case when '+ name +' is null then ''NULL'' else '+'''N'''''' + ' + 'replace('+ name+','''''''','''''''''''')' + '+'''''''''+' end'
when xtype in (175)
then 'case when '+ name +' is null then ''NULL'' else '+''''''''' + ' + 'cast(replace('+ name+','''''''','''''''''''') as Char(' + cast(length as varchar) + '))+'''''''''+' end'
when xtype in (239)
then 'case when '+ name +' is null then ''NULL'' else '+'''N'''''' + ' + 'cast(replace('+ name+','''''''','''''''''''') as Char(' + cast(length as varchar) + '))+'''''''''+' end'
else '''NULL'''
end as Cols,name
from syscolumns
where id = object_id(@tablename)
) T
set @sql ='select ''INSERT INTO '+ @tablename + left(@sql,len(@sql)-1)+') ' + left(@sqlValues,len(@sqlValues)-4) + ')'' from '+@tablename
print @sql
exec (@sql)
end
GO
步骤二、在sql查询分析器中执行:exec spGenInsertSQL 'TableName' ;
步骤三、将查询出来的结果另存。
相关文章推荐
- 将SQLServer中表按其结构生成SQl插入语句
- Sqlserver根据表名生成insert 插入语句的sql
- 好用生成插入语句的SQL脚本
- [sqlserver脚本]查看指定SQL语句生成了哪些执行计划
- python脚本随笔:实现生成插入表的sql语句
- 【转载】将sqlserver表中的数据导出sql语句或生成insert into语句
- 【转载】将sqlserver表中的数据导出sql语句或生成insert into语句
- MYSQL使用SQL语句生成表结构文档语句
- 实体类自动生成SQL插入执行语句
- 把excel数据生成sql插入数据库语句
- SQLServer中查询表结构(表主键 、列说明、列数据类型、所有表名)的Sql语句
- 把SQL数据库生成脚本插入的 SQL语句
- sqlserver 数据库 里面有数据 怎么生成 插入语句 把里面的数据
- 用Sql语句生成Sqlserver数据字典
- 如何将sqlserver表中的数据导出sql语句或生成insert into语句 [转]
- SQl:跨数据库从一个表的数据插入另一个数据库的表数据 生成语句进行插入
- 将sqlserver表中的数据导出sql语句或生成insert into语句
- EF监听生成的所有sql语句(包括插入,删除sql)
- 两个结构完全相同的表a和b,主键为index,使用SQL语句,把a表中存在但在b表中不存在的数据插入的b表中
- 一个自动生成插入与更新SQL语句的小类