sqlserver insert 存储过程
-- 根据表中数据生成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 '表名'
sqlserver insert 存储过程的更多相关文章
- Sqlserver中存储过程,触发器,自定义函数(二)
Sqlserver中存储过程,触发器,自定义函数: 自定义函数:1.函数类型:2.函数的参数和返回值: 1.函数类型:标量值函数,返回的是一个标量值表值函数:内联表值函数:多语句表值函数. 标量值函数 ...
- Sqlserver中存储过程,触发器,自定义函数
Sqlserver中存储过程,触发器,自定义函数: 1. 触发器:是针对数据库表或数据库的特殊存储过程,在某些行为发生的时候就会被激活 触发器的分类: DML触发器:发生在数据操作语言执行时触发执行的 ...
- SQLServer 的存储过程与java交互
一. 存储过程简介 Sql Server的存储过程是一个被命名的存储在服务器上的Transacation-Sql语句集合,是封装重复性工作的一种方法,它支持用户声明的变量.条件执行和其他强大的编程 ...
- SqlServer中存储过程 returnC#代码处理以及对应的MySQL如何改写
一.SqlServer 中 1. 创建表 create table testuser( id int, --primary key, names ), address ), paw ) ) 2.创建存 ...
- Sqlserver中存储过程,触发器,自定义函数(一)
Sqlserver中存储过程,触发器,自定义函数 1.存储过程有关内容存储过程的定义:存储过程的分类:存储过程的创建,修改,执行:存储过程中参数的传递,返回与接收:存储过程的返回值:存储过程使用游标. ...
- JSON序列化及利用SqlServer系统存储过程sp_send_dbmail发送邮件(一)
JSON序列化 http://www.cnblogs.com/yubaolee/p/json_serialize.html 利用SqlServer系统存储过程sp_send_dbmail发送邮件(一) ...
- SqlServer中存储过程中将Exec的执行结果赋值给变量输出
原文 SqlServer中存储过程中将Exec的执行结果赋值给变量输出 背景: 遇到这样一种情况:动态向存储过程中传入表名和表的某些属性(这里用到的是主键ID),然后利用这两个变量查出一条数据的某些字 ...
- SqlServer复杂存储过程
SqlServer复杂存储过程 CREATE PROCEDURE FETCH_GOOUT_INFO AS BEGIN WITH l as(SELECT A.ZJHM, O.KSQR, O.JSRQ, ...
- SQLServer 中存储过程
SQLServer 中存储过程返回的三种方式( 包括存储过程的创建, 在存储过程中调用, 在VS中调用的方法)存储过程有三种返回: 1. 用return返回数字型数据 2. 用返回参数 ...
随机推荐
- lianjie
数值策划入门:如何确定游戏中的资源价值和定价http://bbs.gameres.com/thread_494366.html 一张常规的RPG游戏地图的制作流程 http://bbs.gameres ...
- 动态导入(import)和静态导入(import)的区别
import static静态导入是JDK1.5中的新特性.一般我们导入一个类都用 import com.....ClassName;而静态导入是这样:import static com.....Cl ...
- Asp.net 网站防攻击安全设置
针对已解密的_ViewStat参数漏洞整改建议:在<system.web>下添加 <machineKey validation="3DES"/> 禁用脚本调 ...
- C++中的单例模式(转)
单例模式也称为单件模式.单子模式,可能是使用最广泛的设计模式.其意图是保证一个类仅有一个实例,并提供一个访问它的全局访问点,该实例被所有程序模块共享.有很多地方需要这样的功能模块,如系统的日志输出,G ...
- FineUI第十二天---锚点布局
锚点布局的典型结构: <x:Panel Layout="Anchor" runat="server"> <Items ...
- oracle 彻底删除用户及表空间
1.删除表空间 可以先将其offline alter tablespace xxx offline; 将磁盘上的数据文件一同删除 drop tablespace xxx including conte ...
- .net 默认时间格式不正确
经测试发现,有时通过在“日期.时间.语言和区域设置”里面设置是无效的. 对应键值为HKEY_LOCAL_MACHINE"SOFTWARE"Microsoft"OLEAUT ...
- 5分钟教你Windows 10中将“运行”固定到开始菜单
导读 “运行”功能深受很多资深IT之家用户喜爱,因为它简约.方便.实用.在Win7等旧版系统中,用户可以让该功能直接在开始菜单显示,方便操作.但在Win10中,由于开始菜单已经重新编写,原有的设定已经 ...
- jQuery源码笔记(一):jQuery的整体结构
jQuery 是一个非常优秀的 JS 库,与 Prototype,YUI,Mootools 等众多的 Js 类库相比,它剑走偏锋,从 web 开发的实用角度出发,抛除了其它 Lib 中一些中看但不实用 ...
- 快速排序(Quicksort)
快速排序:是对冒泡排序的一种改进. 什么是冒泡排序:简单一点就是冒气泡.极值数据会到达数据的顶端. 实现步骤:建立一个数据排列标准,从大到小还是从小到大. [从大到小排列]:从第一个数据开始遍历,比较 ...