【SQL Sever】将SQL Sever中的一个数据表的数据导出为insert语句
例如:这SQL Sever中的一张数据表,想要将这张数据表中的数据 转化成一个一个的insert语句存储在txt的文档中,那么不论走到那里这个insert语句一执行,我们就能将这个数据表中的数据插入到另一个地方了。
1》在新建查询中,创建一个对象,这个对象就是用来产生这个对象的,名字叫proc_insert,我们可以创建多个不重名的对象,当然也可以删除这个对象。
1 create proc proc_insert (@tablename varchar(256)) 2 as 3 begin 4 set nocount on 5 declare @sqlstr varchar(4000) 6 declare @sqlstr1 varchar(4000) 7 declare @sqlstr2 varchar(4000) 8 select @sqlstr='select ''insert '+@tablename 9 select @sqlstr1='' 10 select @sqlstr2=' (' 11 select @sqlstr1= ' values ( ''+' 12 select @sqlstr1=@sqlstr1+col+'+'',''+' ,@sqlstr2=@sqlstr2+name +',' from (select case 13 -- 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' 14 when a.xtype =127 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(20),'+a.name +')'+' end' 15 when a.xtype =104 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(1),'+a.name +')'+' end' 16 when a.xtype =175 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end' 17 when a.xtype =61 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'convert(varchar(23),'+a.name +',121)'+ '+'''''''''+' end' 18 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' 19 when a.xtype =62 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(23),'+a.name +',2)'+' end' 20 when a.xtype =56 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(11),'+a.name +')'+' end' 21 when a.xtype =60 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(22),'+a.name +')'+' end' 22 when a.xtype =239 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end' 23 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' 24 when a.xtype =231 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end' 25 when a.xtype =59 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(23),'+a.name +',2)'+' end' 26 when a.xtype =58 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'convert(varchar(23),'+a.name +',121)'+ '+'''''''''+' end' 27 when a.xtype =52 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(12),'+a.name +')'+' end' 28 when a.xtype =122 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(22),'+a.name +')'+' end' 29 when a.xtype =48 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(6),'+a.name +')'+' end' 30 -- 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' 31 when a.xtype =167 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end' 32 else '''NULL''' 33 end as col,a.colid,a.name 34 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 35 )t order by colid 36 37 select @sqlstr=@sqlstr+left(@sqlstr2,len(@sqlstr2)-1)+') '+left(@sqlstr1,len(@sqlstr1)-3)+')'' from '+@tablename 38 -- print @sqlstr 39 exec( @sqlstr) 40 set nocount off 41 end 42 go
View Code
2》执行这个对象,让他产生insert语句
1 exec proc_insert p_phone;
View Code
效果如下:
3》第一步全选,第二步将结果另存为
4》这样就生成了一个文本文件了
5》如果这里面的id是自增的,或者不想让某一列插入,那就将这些代码放在word中进行替换。
END----
转载于:https://www.cnblogs.com/sxdcgaq8080/p/5909300.html
【SQL Sever】将SQL Sever中的一个数据表的数据导出为insert语句相关推荐
- pandas任取dataframe中的一个或者多个数据行(head、tail、loc、iloc),将抽取到的一个或者多个数据行复制N次形成新的dataframe
pandas任取dataframe中的一个或者多个数据行(head.tail.loc.iloc),将抽取到的一个或者多个数据行复制N次形成新的dataframe 目录
- pandas从dataframe中删除一个或多个数据列
pandas从dataframe中删除一个或多个数据列 目录 pandas从dataframe中删除一个或多个数据列 #删除数据列的基本语法
- 将excel中的一个工作表按照某列拆分成多个sheet工作表
如何将excel中的一个工作表按照某列拆分成多个sheet工作表呢?接下来就利用VBA工具来解决这个问题. 拆分之前的工作表: 拆分之后的工作表: 操作步骤具体如下: 第一步:打开需要拆分的表格文件: ...
- Case Study: 利用PHP获取关系型数据库中多张数据表的数据
一.目标 该笔记的目的是引导读者借助WampServer平台和MySQL数据库,利用HTML/CSS/JS/PHP设计一个多数据表关联的网页.在上一个案例(Case Study: 利用JS实现数据库网 ...
- oracle数据库定时同步工具,[每天自动同步一个数据库表的数据]sql server定时同步oracle数据表...
–*********************************************************************** –Copyright (c) 2003 Microso ...
- php sql 去除重复数据,MSSQL_快速删除重复记录,SQL Server如何实现?,如果一张表的数据达到上百万 - phpStudy...
如果一张表的数据达到上百万条,用游标的方法来删除简直是个噩梦,因为它会执行相当长的一段时间-- 开发人员的噩梦--删除重复记录 想必每一位开发人员都有过类似的经历,在对数据库进行查询或统计的时候不时地 ...
- SQL Server 与 Excel,Access 数据表的导入导出(注:参照博园.NET大观)
我们知道在Sql Server 中 集成了数据的导入导出这么一个工具,那用Sql 脚本怎么轻松操作 Access 和 Excel 中的数据呢,接下来我们看一看: 一. SQL SERVER 和EXCE ...
- Navicat 数据表的数据的添加,删除及使用sql完成数据的添加。
Navicat创建数据库及表的创建,删除和插入._星星以外的博客-CSDN博客 数据库主键的作用及添加_星星以外的博客-CSDN博客 选中操作的表,右击----打开表(或者直接双击也行) 则出现如下界 ...
- Office EXCEL 中如何让一个单元格的数据链接到另一个工作表的数据
比如我在Sheet2中定义了几个数据,这些都是简单的数字,而在Sheet1中让要被绑定的单元格等于Sheet2的对应单元格地址(比如Sheet2!B1,Sheet2!B2之类的) 然后就可以一改全改了 ...
最新文章
- 简单三步搭建一对一直播源码系统
- Kubernetes 1.14重磅来袭,多项关键特性生产可用
- SQL5 将查询后的列重新命名(数据库的几种去重方法)
- 小程序开发初体验,从静态demo到接入Bmob数据库完全实现
- OJ1048: 阶乘表(C语言)(易错,关于int和double范围问题)
- 【Openstack】实录手动部署Openstack Rocky 双节点(6)- Horizon
- 麦步手表编程纪实(1)
- 扬州打工人租房编年史
- 计算机金融专业课程方案,《计算机在金融业中的应用》教学实施方案
- 孙鑫VC学习笔记:第十九讲 动态链接库
- 理解AutoResetEvent与ManualResetEvent的区别
- 计算机专业学微机原理与接口技术,信息技术学院计算机科学与技术专业《微机原理与接口技术.doc...
- 容斥原理与Mobius函数
- Si5395/94/92时钟芯片配置步骤
- 电脑开机提示计算机无法启动不了,图文详解电脑开机无法进入系统怎么办
- log4j2漏洞各大厂应对措施
- 虚拟机一插U盘或读卡器就死机的解决办法
- android手机如何加速,小技巧:如何给Android手机上的Chrome浏览器加速
- Win10 升级后不能正常使用谷歌浏览器
- 关于C++中Eigen库效率提升的思考
热门文章
- ExtJS 4 Beta 2预览:Ext.Brew包
- 在storm中使用定时保存
- python二维图颜色函数_Python scipy的二维图像卷积运算与图像模糊处理操作示例
- 如何更改服务器上的数据库文件夹,如何设置数据库文件位置
- Spring模板对象
- 河北体检系统诚信企业推荐_应用多的隔膜计量泵价格诚信企业推荐
- jquery 树形框 横_利用jQuery设计横/纵向菜单
- php后门 佛像,35张活的再久,也未必见过的照片,图2是佛像通过CT扫描后内部照...
- java word在线预览_java 生成word文档并且在线预览的问题
- JMS学习八(ActiveMQ消息持久化)