根据表中数据生成insert语句的存储过程
来源:互联网 发布:淘宝客服回答问题技巧 编辑:程序博客网 时间:2024/05/22 10:28
-- 根据表中数据生成insert语句的存储过程
Alter Proc proc_insert (@tablename varchar(256), @wherestr nvarchar(1000))
as -- 表名称
begin
set nocount on
Declare @sqlstr varchar(4000),
@sqlstr1 varchar(4000),
@sqlstr2 varchar(4000)
Select @sqlstr='select ''Insert into '+@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
and COLUMNPROPERTY( a.id,a.name,'IsIdentity') <> 1)t order by colid
select @sqlstr=@sqlstr+left(@sqlstr2,len(@sqlstr2)-1)+') '+left(@sqlstr1,len(@sqlstr1)-4)+')'' from '+@tablename + ' ' + @wherestr
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语句的存储过程
- 利用webservice和.net技术上传和下载文件--Sending files in chunks with MTOM Web Services and .NET 2.0 from:http://www.codeproject.com/soap/MTO
- js实现页面跳转的几种方式
- [转]设置检查文本框中为数字方法。
- sql server行转列问题终极解决
- JAVA程序员,如果你没有用过Sourceforge.net找点开源工具用用,不管你怎么样,我都认为你不是很合格
- 根据表中数据生成insert语句的存储过程
- 存储过程介绍及asp存储过程调用
- Ria介绍
- 怎么样用GHOST备份XP/VISTA系统
- Firefox播放mp3要安装quicktime问题
- java简史
- 列出SQL SERVER 所有表、字段定义,类型,长度,一个值等信息
- 列出SQL SERVER 所有表,字段名,主键,类型,长度,小数位数等信息
- 踏雪有痕-变量初始化