sql server根据表中数据生成insert语句

几个收藏的根据数据库生成Insert语句的存储过程[修正版]
复制代码
-- ======================================================
--
根据表中数据生成insert语句的存储过程
--
建立存储过程,执行spGenInsertSQL 表名
--
感谢playyuer
--
--感谢szyicol
--
======================================================
CREATE proc [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
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

-- ======================================================
--
根据表中数据生成insert语句的存储过程
--
建立存储过程,执行proc_insert 表名
--
感谢Sky_blue
--
感谢szyicol
--
======================================================
CREATE proc [dbo].[proc_insert] (@tablename varchar(256))
as
begin
set nocount on
--declare @tablename varchar(256)
--set @tablename = 'AD'
declare @sqlstr varchar(4000)
declare @sqlstr1 varchar(4000)
declare @sqlstr2 varchar(4000)
select @sqlstr='select ''insert '+@tablename

select @sqlstr1=''
select @sqlstr2=' ('
select @sqlstr1= ' values ( ''+'
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
--print @sqlstr
exec( @sqlstr)
set nocount off
end
复制代码

原文:几个收藏的根据数据库生成Insert语句的存储过程

修正了表中的字段如果是SQL中的关键字(如Order)时,生成的脚本执行会出错的bug

转载于:https://www.cnblogs.com/51net/archive/2013/04/01/2992875.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/376770.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Javascript eval()函数 基础回顾

如果您想详细了解ev al和JSON请参考以下链接&#xff1a; eval &#xff1a;https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Global_Functions/Eval JSON&#xff1a;http://www.json.org/ eval函数的工作原理 eval函数会评估一个给定的含有JavaScript代码的…

杂感无题|

今天中午和组里面的人吃饭&#xff0c;聊起了科兴跳楼的事情。这事其实前几天我华为的mentor就转给我了&#xff0c;当时也没太在意&#xff0c;在脉脉上看了看&#xff0c;也不知晓是谁&#xff0c;想着可能又是抑郁症吧。 饭后依旧绕着食堂散步&#xff0c;ly说那个人好像还是…

uva1366_Martian Mining_简单DP

题目不难&#xff0c;却想了好长时间&#xff0c;目测自己DP还是很水。。。囧 思路&#xff1a;舍f[i][j]为前i行j列的最大矿总量不难推出状态转移方程为f[i][j]max(f[i-1][j]line[i][j],f[i][j-1]row[j][i]) 其中line[i][j]为第i行前j个A矿的和&#xff08;a[i][1]a[i][2]...a…

数学图形之Boy surface

这是一个姓Boy的人发现的,所以取名为Boy surface.该图形与罗马图形有点相似,都是三分的图形.它甚至可以说是由罗马曲面变化而成的. 本文将展示几种Boy曲面的生成算法和切图,使用自己定义语法的脚本代码生成数学图形.相关软件参见:数学图形可视化工具,该软件免费开源.QQ交流群: …

开个定时器给echarts组件配置定时更新

我在js文件中开了个定时器&#xff0c;每1s从后端获取数据并解析&#xff0c;然后用异步方法就渲染不出来&#xff0c;改成同步就可以了。 这个解决方法来自于这篇文章&#xff0c;我出的问题和他一样&#xff1a;关于ajax中readyState的值一直为1的问题 这里将ajax参数修改为f…

SDK 操作 list-view control 实例 -- 遍历进程

遍历窗口&#xff0c;获得控件句柄 1 EnumChildWindows(hwndDlg, (WNDENUMPROC)EnumChildProc, NULL); 回调函数 1 BOOL CALLBACK EnumChildProc(HWND hwnd, LPARAM lParam )2 {3 char strCLSName[MAXBYTE] {0};4 GetClassName(hwnd, strCLSName, MAXBYTE);5 if (…

推荐一份不错的清除默认样式的CSS样式

时间过得真快&#xff0c;离 Reset CSS 研究&#xff08;八卦篇&#xff09; 已经 3 个多月了。废话少说&#xff0c;赶紧将技术篇写完吧。 回顾与反思 第一份 reset css 是 Tantek 的 undohtml.css, 很简单的代码&#xff0c;Tantek 根据自己的需要&#xff0c;对浏览器的默认…

python深浅拷贝

在python中&#xff0c;对象赋值实际上是对象的引用。当创建一个对象&#xff0c;然后把它赋给另一个变量的时候&#xff0c;python并没有拷贝这个对象&#xff0c;而只是拷贝了这个对象的引用。 所以一个结构类型被赋给另外一个对象的时候&#xff0c;尽可能不使用 &#xff…

Flash中的SLC/MLC/MLC--基础

参考 1.http://www.upantool.com/jiaocheng/qita/2012/slc_mlc_tlc.html 2.http://www.2ic.cn/html/10/t-432410.html 3.http://kms.lenovots.com/kb/article.php?id15382 4.http://www.albertknight.com/222.html 5.http://ssd.zol.com.cn/371/3716632.html 6.这个图比较多 h…

python定义对象的比较方法

有时候我们需要比较两个对象。比如哪个对象大,哪个对象小。如果我们不告诉python如何比较,那么Python是不知道如何进行比较的。 下面提供实例 #__eq__(self,other)&#xff1a; #在使用比较运算符比较两个对象是否相等的时候会调用这个方法。 #如果是相等&#xff0c;那么应该返…

关于Oracle Insert 语句的子查询 和 with check option的用法

今日睇ocp教程 发现 insert语句还可以子查询例如&#xff1a;INSERT INTO (SELECT employee_id, last_name, email, hire_date, job_id, salary, department_id FROM employees where department_id 50 )VALUES (9999…

apple mac 下使用机械键盘的办法,键盘映射工具软件,apple mac Mechanical keyboard

apple mac 下使用机械键盘的办法&#xff0c;键盘映射工具软件&#xff0c;apple mac Mechanical keyboard 想在苹果电脑 mac 系统下使用 机械键盘&#xff0c;大部分机械键盘不是为mac设计的&#xff0c;所以需要用软件做一下键盘映射。 推荐使用这个&#xff1a;https://pqrs…

Python中键映射多个值的方法:defaultdict

Python中键映射多个值的方法有两种&#xff1a; 想保持元素的插入顺序就应该使用列表&#xff1b; 想去掉重复元素就使用集合并且不关心元素的顺序问题的话应该使用set from collections import defaultdictmapping defaultdict(list)mapping [key].append(value)mapping d…

该不该让Google收录WordPress的目录页和标签页?

只要有一点SEO知识的 站长都会注意利用相关文件和元标签来控制Google对网站的收录&#xff0c;对于WordPress网站来说&#xff0c;除了我们主动添加的内容页面&#xff0c;Google还会收录目录归档页&#xff0c;标签归档页&#xff0c;时间归档页&#xff0c;以及作者归档页。这…

【原创】MapReduce编程系列之表连接

问题描述需要连接的表如下&#xff1a;其中左边是child&#xff0c;右边是parent&#xff0c;我们要做的是找出grandchild和grandparent的对应关系&#xff0c;为此需要进行表的连接。 Tom Lucy Tom Jim Lucy David Lucy Lili Jim Lilei Jim SuSan Lily Green Lily Bians Green…

python logging模块简单使用

logging 是线程安全的&#xff0c;也就是说&#xff0c;在一个进程内的多个线程同时往同一个文件写日志是安全的。 但是多个进程往同一个文件写日志不是安全的。 import loggingLOG_FORMAT "%(asctime)s - %(levelname)s - %(message)s" DATE_FORMAT "%m/%d/…

OpenACC 中parallel 和kernels的区别

Kernels构件 Kernels构件源于PGI Accelerator模型的region构件。嵌套kernels构件里的循环可能会被编译器转换成能在GPU上高效并行的部分。在这个过程中有三步。 1&#xff1a;判断并行中遇到的循环。 2&#xff1a;把抽象的并行转换成硬件上的并行。对于NVIDIA CUDA GPU&#…

ORACLE基本SQL语句-查询篇

一、普通查询 /*查询表数据*/select * from STU /*取出前3行数据*/select * from stu where ROWNUM<3 /*模糊查询*/select * from stu where stu_id like stu001% 说明&#xff1a;通配符“%”代表一个或者多个字符&#xff0c;通配符“_”代表一个字符。 /*别名*/select S…

三次握手建立失败的几种情况以及三次握手的理解

上面的图是阻塞式socket进行通信的过程&#xff0c;阻塞的时候是操作系统内核网络协议栈在工作 调用 connect 函数将激发 TCP 的三次握手过程&#xff0c;而且仅在连接建立成功或出错时才返回。其中出错返回可能有以下几种情况&#xff1a; 1、三次握手无法建立&#xff0c;客…

db_name,instance_name,service_names,db_domain,dbid,oracle_sid等区别与联系

最近整理了一篇文章&#xff1a;oracle listener 有网友对数据库是否显式设置了instance_name和service_names提出疑问。 由此引发出db_name,instance_name,oracle_sid等等这些常见的参数都代表什么意思&#xff0c;怎么取值的&#xff0c;有什么区别&#xff1f; SQL> sele…