根据表中数据生成insert语句的存储过程
来源:互联网 发布:网络大学有哪些学院 编辑:程序博客网 时间:2024/05/18 21:43
-- 根据表中数据生成insert语句的存储过程
Create Proc proc_insert (@tablename varchar(256))
as -- 表名称
begin
set nocount on
Declare @sqlstr varchar(4000),
@sqlstr1 varchar(4000),
@sqlstr2 varchar(4000)
Select @sqlstr='select ''Insert '+@tablename
Select @sqlstr1= ' Values ( ''+', @sqlstr2=' ('
Select @sqlstr1=@sqlstr1+col+'+'',''+' ,@sqlstr2=@sqlstr2+name +',' from(Select case
-- when a.xtype =173 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.length*2+2)+'),'+a.name +')'+' end'
when a.xtype =104 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(1),'+a.name +')'+' end'
when a.xtype =175 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end'
when a.xtype =61 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'convert(varchar(23),'+a.name +',121)'+ '+'''''''''+' end'
when a.xtype =106 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.xprec+2)+'),'+a.name +')'+' end'
when a.xtype =62 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(23),'+a.name +',2)'+' end'
when a.xtype =56 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(11),'+a.name +')'+' end'
when a.xtype =60 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(22),'+a.name +')'+' end'
when a.xtype =239 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end'
when a.xtype =108 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.xprec+2)+'),'+a.name +')'+' end'
when a.xtype =231 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end'
when a.xtype =59 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(23),'+a.name +',2)'+' end'
when a.xtype =58 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'convert(varchar(23),'+a.name +',121)'+ '+'''''''''+' end'
when a.xtype =52 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(12),'+a.name +')'+' end'
when a.xtype =122 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(22),'+a.name +')'+' end'
when a.xtype =48 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(6),'+a.name +')'+' end'
-- when a.xtype =165 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.length*2+2)+'),'+a.name +')'+' end'
when a.xtype =167 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end'
else '''NULL'''
end as col,a.colid,a.name
from syscolumns a where a.id = object_id(@tablename) and a.xtype <>189 and a.xtype <>34 and a.xtype <>35 and a.xtype <>36
)t order by colid
select @sqlstr=@sqlstr+left(@sqlstr2,len(@sqlstr2)-1)+') '+left(@sqlstr1,len(@sqlstr1)-3)+')'' from '+@tablename
exec( @sqlstr)
set nocount off
end
GO
-- 调用
exec proc_insert '表名'
- 根据表中数据生成insert语句的存储过程
- 根据表中数据生成insert语句的存储过程
- 根据表中数据生成insert语句的存储过程
- 根据表中数据生成insert语句的存储过程
- (转)根据表中数据生成insert语句的存储过程
- (转)根据表中数据生成insert语句的存储过程(另一种方法)
- SQLSERVER表数据生成INSERT语句(通用存储过程)
- 生成insert语句的存储过程
- 生成表insert into语句的存储过程
- 将表中的数据生成Insert脚本的存储过程!!!
- 一个把表中数据生成Insert脚本的通用存储过程
- mssql 生成‘批量Insert数据的sql语句’的存储过程
- 自动生成INSERT语句的SQL存储过程
- 生成Insert语句SQL存储过程
- SQL Server 存储过程生成insert语句
- 根据基本表结构及其数据生成INSERT 语句的SQL脚本
- 导出数据库数据记录成insert语句的存储过程
- 将数据库数据导出成insert语句的存储过程
- J2ME 实现电话呼叫
- Can not issue data manipulation statements with executeQuery()错误解决
- iis 500 服务器错误 不能解析asp文件的解决方法
- c语言 数组 指针
- J2SE5.0新特性
- 根据表中数据生成insert语句的存储过程
- J2ME中利用MMAPI播放声音
- Java数据库编程中的几个常用技巧
- JBOSS4 数据源配置大全
- [吐血推荐] 比例奇观!以惊人的方式看世界!
- 第一章 练习题(07.3.5)
- AJAX基本应用之:根据输入内容自动填充下拉框。(ASP.NET版)[转]
- 一个美国老工程师的心理话: 给年轻工程师的十大忠告
- 搜索引擎查询层的主要检索质量优化的方法,在参加完google的互联网大会后的总结