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

MSSQL的表备份成INSERT脚本的存储过程的更多相关文章

  1. 将表数据生成Insert脚本

    set ANSI_NULLS ONset QUOTED_IDENTIFIER ONgo-- =============================================-- Author ...

  2. 自由导入你的增量数据-根据条件将sqlserver表批量生成INSERT语句的存储过程实施笔记

    文章标题: 自由导入你的增量数据-根据条件将sqlserver表批量生成INSERT语句的存储过程增强版 关键字 : mssql-scripter,SQL Server 文章分类: 技术分享 创建时间 ...

  3. 实现对MySQL数据库进行分库/分表备份(shell脚本)

    工作中,往往数据库备份是件非常重要的事情,毕竟数据就是金钱,就是生命!废话不多,下面介绍一下:如何实现对MySQL数据库进行分库备份(shell脚本) Mysq数据库dump备份/还原语法: mysq ...

  4. MSSQL数库备份与还原脚本(多个库时很方便)

    每次通过 Management Studio 的界面操作备份或还原数据库,对于单个数据库还好,要是一次要做多个.那就还是用脚本快些,下面有两段脚本分享一下. ===================== ...

  5. SqlServer 导出指定表数据 生成Insert脚本

    版权声明:本文为博主原创文章,未经博主允许不得转载.

  6. sql server 数据库导出表里所有数据成insert 语句

    有时候,我们想把数据库的某张表里的所有数据导入到另外一个数据库或另外一台计算机上的数据库,对于sql server有这样的一种方法 下面我以sql server 2008 R2,数据库是Northwi ...

  7. [Mysql]备份同库中一张表的历史记录 insert into ..select

    需求 现在有个这么一个需求,mysql中有个表,数据增长的很快,但是呢这个数据有效期也就是1个月,一个月以前的记录不太重要了,但是又不能删除.为了保证这个表的查询速度,需要一个简单的备份表,把数据倒进 ...

  8. 分享一个MySQL分库分表备份脚本(原)

    分享一个MySQL分库备份脚本(原) 开发思路: 1.路径:规定备份到什么位置,把路径(先判断是否存在,不存在创建一个目录)先定义好,我的路径:/mysql/backup,每个备份用压缩提升效率,带上 ...

  9. MySQL分库分表备份脚本

    MySQL分库备份脚本 #脚本详细内容 [root@db02 scripts]# cat /server/scripts/Store_backup.sh #!/bin/sh MYUSER=root M ...

随机推荐

  1. codevs3728联合权值(LCA)

    3728 联合权值  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Description 输入描述 Input Des ...

  2. keystone身份认证服务

    Keystone介绍 keystone 是OpenStack的组件之一,用于为OpenStack家族中的其它组件成员提供统一的认证服务,包括身份验证.令牌的发放和校验.服务列表.用户权限的定义等等.云 ...

  3. Akka源码分析-Remote-Actor创建

    在之前的博客中,我们分析过local模式下Actor的创建过程,最终还是调用了provider的actorOf的函数创建了Actor,在remote模式下provider就是RemoteActorRe ...

  4. Elasticsearch之CURL命令的bulk批量操作

    大家,也可去看看我下面的博客 Elasticsearch之批量操作bulk 官网上,是举例了新建一个requests文件. [hadoop@master elasticsearch-]$ pwd /h ...

  5. Unicode gbk gb2312 编码问题 [转载]

    原文地址: http://www.cnblogs.com/csn0721/archive/2013/01/24/2875613.html HTML5 UTF-8 中文乱码   <!DOCTYPE ...

  6. JAVA FORK JOIN EXAMPLE--转

    http://www.javacreed.com/java-fork-join-example/ Java 7 introduced a new type of ExecutorService (Ja ...

  7. 【sqli-labs】 less57 GET -Challenge -Union -14 queries allowed -Variation4 (GET型 挑战 联合查询 只允许14次查询 变化4)

    双引号闭合 http://192.168.136.128/sqli-labs-master/Less-57/?id=1"%23 和less56一样查数据

  8. c#仿照qq登录界面编辑框内容操作

    using System; using System.Drawing; using System.Windows.Forms; namespace 案例演示 { public partial clas ...

  9. 解决 The file will have its original line endings in your working directory

    首先出现这个问题主要原因是:我们从别人github地址上通过git clone下载下来,而又想git push到我们自己的github上,那么就会出现上面提示的错误信息 此时需要执行如下代码: git ...

  10. viod 0是什么?

    之前在牛客网上看到别人用viod 0来代替undefined,所以我去网上搜了一下,MDN是这么说的: 这个运算符能向期望一个表达式的值是undefined的地方插入会产生副作用的表达式. void ...