将表中数据转换成Insert语句
2012-02-01 10:47
155 查看
/****** Object: StoredProcedure [dbo].[spGenInsertSQL] Script Date: 02/01/2012 10:48:38 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[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
-- select * from systypes 来查看各类型对应号码
when xtype in (48,52,56,59,60,62,104,106,108,122,127) --Int,Real
then 'case when '+ name +' is null then ''NULL'' else ' + 'cast('+ name + ' as varchar)'+' end'
when xtype in (58,61) --datetime,samlldatetime
then 'case when '+ name +' is null then ''NULL'' else '+''''''''' + ' + 'cast('+ name +' as varchar)'+ '+'''''''''+' end'
when xtype in (167) --varchar
then 'case when '+ name +' is null then ''NULL'' else '+''''''''' + ' + 'replace('+ name+','''''''','''''''''''')' + '+'''''''''+' end'
when xtype in (231)--nvarchar
then 'case when '+ name +' is null then ''NULL'' else '+'''N'''''' + ' + 'replace('+ name+','''''''','''''''''''')' + '+'''''''''+' end'
when xtype in (175) --char
then 'case when '+ name +' is null then ''NULL'' else '+''''''''' + ' + 'cast(replace('+ name+','''''''','''''''''''') as Char(' + cast(length as varchar) + '))+'''''''''+' end'
when xtype in (239) --nchar
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 autoval is null and id = object_id(@tablename) and xtype <>189 and xtype <>34 and xtype <>35 and xtype <>36
) T
set @sql ='select ''INSERT INTO ['+ @tablename + ']' + left(@sql,len(@sql)-1)+') ' + left(@sqlValues,len(@sqlValues)-4) + ')'' from '+@tablename
--print @sql
exec (@sql)
end
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[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
-- select * from systypes 来查看各类型对应号码
when xtype in (48,52,56,59,60,62,104,106,108,122,127) --Int,Real
then 'case when '+ name +' is null then ''NULL'' else ' + 'cast('+ name + ' as varchar)'+' end'
when xtype in (58,61) --datetime,samlldatetime
then 'case when '+ name +' is null then ''NULL'' else '+''''''''' + ' + 'cast('+ name +' as varchar)'+ '+'''''''''+' end'
when xtype in (167) --varchar
then 'case when '+ name +' is null then ''NULL'' else '+''''''''' + ' + 'replace('+ name+','''''''','''''''''''')' + '+'''''''''+' end'
when xtype in (231)--nvarchar
then 'case when '+ name +' is null then ''NULL'' else '+'''N'''''' + ' + 'replace('+ name+','''''''','''''''''''')' + '+'''''''''+' end'
when xtype in (175) --char
then 'case when '+ name +' is null then ''NULL'' else '+''''''''' + ' + 'cast(replace('+ name+','''''''','''''''''''') as Char(' + cast(length as varchar) + '))+'''''''''+' end'
when xtype in (239) --nchar
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 autoval is null and id = object_id(@tablename) and xtype <>189 and xtype <>34 and xtype <>35 and xtype <>36
) T
set @sql ='select ''INSERT INTO ['+ @tablename + ']' + left(@sql,len(@sql)-1)+') ' + left(@sqlValues,len(@sqlValues)-4) + ')'' from '+@tablename
--print @sql
exec (@sql)
end
相关文章推荐
- excel数据转换成insert语句
- 将数据库中的数据转换为insert语句
- 将Excel表格的数据转换成sql的insert语句
- 将Excel中的数据转换成sql Insert语句
- 将pgadmin导出的数据转换成INSERT语句
- 通过Python将MongoDB导出的json数据转换成Mysql的insert语句导入
- 把SQL Server数据表的内容转换为相应的INSERT语句
- 将Excel中的数据转换成sql Insert语句
- python将json格式数据转换为insert into语句导入mysql数据库
- 表数据转换为insert语句
- 将数据库中的数据转换为insert语句
- 将excel中的数据转换成insert语句
- 将数据库中的数据转换为insert语句
- 将Excel中的数据转换成sql Insert语句
- SQL2005把数据转换成insert语句的存储过程
- EXCEL表里的数据转换成insert into tablename(field)values() SQL插入语句
- 根据表中已存在的数据自动转换成 insert 语句
- 将Excel中的数据转换成sql Insert语句
- 关于sql条件语句where id in (@参数)执行报错问题(转换成数据类型 int 时失败)(
- sqlserver 将数据导成insert语句的存储过程 整库