MSSQL的表备份成INSERT脚本的存储过程
USE [SupplyChain]
GO
/****** Object: StoredProcedure [dbo].[ExpData] Script Date: 2015-12-18 10:23:08 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:
-- Create date:
-- Description: 将表数据生成Insert脚本
-- Demo : exec expdata 'sys_plugin','1=1'
-- exec expdata 'sys_power','1=1'
-- =============================================
ALTER proc [dbo].[ExpData] (@tablename varchar(256),@con nvarchar(400))
as
begin
set nocount on
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 =36 then 'case when ['+a.[name]+'] is null then ''NULL'' else '+'''''''''+'+'replace('+a.[name]+','''''''','''''''''''')' + '+'''''''''+' end'
--when a.xtype =36 then 'case when ['+a.[name]+'] is null then ''NULL'' else '+'convert(varchar(250),['+a.[name] +'])'+' end'
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 =127 then 'case when ['+a.[name]+'] is null then ''NULL'' else '+'convert(varchar(6),['+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+']' as 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 + ' where ' + @con
print @sqlstr
exec( @sqlstr)
set nocount off
end
转载于:https://www.cnblogs.com/hnxxcxg/p/5056281.html
MSSQL的表备份成INSERT脚本的存储过程相关推荐
- 将表数据生成Insert脚本
-- ============================================= -- Author: 明永成 -- Create date: 2016-01-03 -- Descri ...
- 将表数据生成SQL脚本的存储过程和工具
将表数据生成SQL脚本的存储过程示例: CREATE PROCEDURE dbo.UspOutputData @tablename sysname AS declare @column varchar ...
- excel数据表转成insert语句插入数据库
有时候得到的是一张数据图表,要将这张图表完完全全复制粘贴到数据库中是一个比较复杂的事情,如果只是一个晓得数据库,还可以一行一行的输入,但是如果是一张大的数据表的话,这个工作量就会非常大,现在为大家介绍 ...
- SqlServer 导出指定表数据 生成Insert脚本
版权声明:本文为博主原创文章,未经博主允许不得转载. 转载于:https://www.cnblogs.com/firstdream/p/7285157.html
- 【mysql dump】 备份原理及脚本
导出多张表的时候表之间用空格分开: # mysqldump -h192.168.254.102 -uroot test testaa testbb >zbb.sql 错误写法: # mysqld ...
- 将mysql查询语句导出insert_sql server 数据库导出表里所有数据成insert 语句
有时候,我们想把数据库的某张表里的所有数据导入到另外一个数据库或另外一台计算机上的数据库,对于sql server有这样的一种方法 下面我以sql server 2008 R2,数据库是Northwi ...
- mysql备份还原(视图、存储过程)
最近在备份还原mysql的时候发现,视图还原报错,无法创建视图,在网上查了下资料,找到以下信息: 1.如果备份的数据库含有视图,还原时需要把my.ini中的character-set改为latin1, ...
- [Mysql]备份同库中一张表的历史记录 insert into ..select
需求 如今有个这么一个需求.mysql中有个表.数据增长的非常快.可是呢这个数据有效期也就是1个月,一个月曾经的记录不太重要了,可是又不能删除.为了保证这个表的查询速度,须要一个简单的备份表,把数据倒 ...
- mysql数据库分表备份脚本_MySQL分库分表备份数据库脚本的操作
MySQL分库分表备份数据库脚本的操作 发布时间:2020-05-26 16:52:23 来源:51CTO 阅读:134 作者:三月 本篇文章给大家主要讲的是关于MySQL分库分表备份数据库脚本的操作 ...
- 企业Shell实战-MySQL分库分表备份脚本
本文来自 http://www.xuliangwei.com/xubusi/252.html 免费视频讲解见 http://edu.51cto.com/course/course_id-5064.ht ...
最新文章
- 存储mysql数据存在特殊字符时处理_转义 存储数据时特殊符号的处理
- linux 下/proc/cpuinfo三级缓存,linux /proc/cpuinfo文件分析
- 20155335俞昆 2017-2018-1 《信息安全系统设计》第9周学习总结
- 使用Timer执行定时任务
- mysql 存储过程案列一个。
- 微信小程序----评价系统中的评星
- 推荐几个对编程有帮助的游戏
- linux 如何看图软件,深度看图(linux看图软件) v1.2 官方最新版
- Swift游戏实战-跑酷熊猫 08 产生源源不断的移动平台
- 【小程序源码】游戏助手王者荣耀战力查询,游戏扫码登录器等
- 终极.NET混淆器丨.NET Reactor产品介绍
- 求生之路服务器租用配置要求介绍
- 那个直播软件有软件测试课程,资讯详情-直播软件有哪些测试方法-柠檬班-自动化测试-软件测试培训-自学官网...
- 我家的三个犹太小富豪(转)
- 原始的ajaxtookit的使用
- php星期代码,PHP星期几获取代码
- 知识图谱的经典知识库总结
- 工伤事故申请工伤认定多久会生效
- Subtext3 - 安装配置
- webservice 调用国内航班,天气预报 等外部接口
热门文章
- keras实现nlp
- 【286页干货】一天搞懂深度学习(台湾资料科学年会课程)李宏毅
- 深度学习(四):高级卷积神经网络
- 银联公钥加密java实现_【技术贴】银联加密算法 (收藏版)
- 2021-06-26数组详解
- 登录服务器信息怎么删除,服务器端删除了用户,怎么通知客户端浏览器删除cookie...
- fisco bcos DagTransferUser.java 源码解析阅读注释
- Kubernetes 小白学习笔记(5)--Kubernetes集群的部署service、部署deployment、自动负载均衡、自动伸缩、版本升级、版本回退
- linux ffmpeg插件,Linux FFmpeg(含x264、lame插件)安装记录
- HTML5绘制一个3D立方体,HTML5/Canvas交互式3D立方体