sql server 将数据库表里面的数据,转为insert语句,方便小批量转移数据
create proc [dbo].[proc_insert] (@tablename varchar(256))
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 =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 =127 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(20),'+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
先在数据库创建以上存储过程,然后执行如下语句:
exec proc_insert 'MyTableName'
就会得到一批insert 语句,复制到其他地方去执行就可以了,有时候id是identity的不允许显示指定的话,像下面这样子执行
set identity_insert 表名 ON--打开 insert语句...
insert语句... set identity_insert 表名 OFF--关闭
sql server 将数据库表里面的数据,转为insert语句,方便小批量转移数据的更多相关文章
- sql server 数据库导出表里所有数据成insert 语句
有时候,我们想把数据库的某张表里的所有数据导入到另外一个数据库或另外一台计算机上的数据库,对于sql server有这样的一种方法 下面我以sql server 2008 R2,数据库是Northwi ...
- SQL Server 2012 数据库数据导出为脚本
转自:http://blog.csdn.net/ituff/article/details/8265822 将高版本的的SQL Server数据库移到低版本的SQL Server是一件十分蛋疼的事,最 ...
- [转]实战 SQL Server 2008 数据库误删除数据的恢复
实战 SQL Server 2008 数据库误删除数据的恢复 关键字:SQL Server 2008, recover deleted records 今天有个朋友很着急地打电话给我,他用delete ...
- SQL Server 2008 数据库误删除数据的恢复
原文:SQL Server 2008 数据库误删除数据的恢复 原文:http://www.cnblogs.com/dudu/archive/2011/10/15/sql_server_recover_ ...
- sql server的数据库个数、表个数及表的数据量统计
sql server的数据库个数.表个数及表的数据量统计 --由于今天要监控数据,急需统计实例中1有多少库2库里有多少表3每个表有多少数据 --将写好的代码贴出来,用到如下的: --sysobje ...
- Linux下使用FreeTDS访问MS SQL Server 2005数据库(包含C测试源码)
Linux下使用FreeTDS访问MS SQL Server 2005数据库(包含C测试源码) http://blog.csdn.net/helonsy/article/details/7207497 ...
- c# datagridview与DataSet绑定, 列与数据库表里面的列一一对应
参考代码1: 自己模拟出数据,并分别对dataGridView赋值. using System; using System.Collections.Generic; using System.Comp ...
- SQL server 2008数据库的备份与还原、分离(转)
SQL server 2008数据库的备份与还原.分离(转) 一.SQL数据库的备份: 1.依次打开 开始菜单 → 程序 → Microsoft SQL Server 2008 → SQL Ser ...
- 微软ASP.NET网站部署指南(2):部署SQL Server Compact数据库
1. 综述 对于数据库訪问,Contoso University程序要求以下的软件必须随程序一起部署.由于不属于.NET Framework: SQL Server Compact (数据库引擎) A ...
- Sql Server 2008R2 数据库发布与订阅
背景描述: 发布服务器A: (远程端) , 数据库服务名: GUANWANG1 订阅服务器B: (本机) , 数据库服务名: PC-LLRDBA 需要从服务器A中数据库发布,然后在B中订阅A发布 ...
随机推荐
- SpringBoot集成ShardingSphere分表中间件
ShardingSphere简介 ShardingSphere 由 JDBC.Proxy 和 Sidecar(规划中)这 3 款既能够独立部署,又支持混合部署配合使用的产品组成. 它们均提供标准化的基 ...
- Mac 设置多个版本JDK
控制台: p.p1 { margin: 0; font: 11px Menlo; color: rgba(0, 0, 0, 1) } span.s1 { font-variant-ligatures: ...
- 机器学习策略篇:详解如何使用来自不同分布的数据,进行训练和测试(Training and testing on different distributions)
如何使用来自不同分布的数据,进行训练和测试 深度学习算法对训练数据的胃口很大,当收集到足够多带标签的数据构成训练集时,算法效果最好,这导致很多团队用尽一切办法收集数据,然后把它们堆到训练集里,让训练的 ...
- 《从零开始学习Python爬虫:顶点小说全网爬取实战》
顶点小说 装xpath helper GitHub - mic1on/xpath-helper-plus: 这是一个xpath开发者的工具,可以帮助开发者快速的定位网页元素. Question:加载完 ...
- 解决方案 | 如何解决subprocess.Popen(cmd)代码中含有空格路径的问题?
一.背景 因为在python中需要用到subprocess.Popen(cmd),其中cmd由一堆连接的字符串构成:譬如,xxx.exe inputdir outputdir -arg1 -arg2 ...
- git http(s) 保存用户密码
git 常用配置 git记住密码 1.设置记住密码(默认15分钟): git config --global credential.helper cache 2.如果想自己设置时间,可以这样做: gi ...
- MySQL 递归查询实践总结
MySQL复杂查询使用实例 By:授客 QQ:1033553122 表结构设计 SELECT id, `name`, parent_id FROM `tb_testcase_suite` 说明: ...
- 单细胞测序最好的教程(十四)测序原始数据公开至NCBI数据库
作者按 国内对于单细胞测序相关的中文教程确实不够全面,当然NCBI官网给的上传教程也比较详细了,所以变成了会者不难.本教程你现在可能用不上,但是你如果做单细胞测序,那么未来你一定会用上,建议收藏. 在 ...
- c#写一个WINFORM的多线程操作
以下是一个简单的示例,展示了如何在C# WinForms中创建一个按钮的异步事件,并使用Label控件来显示事件执行的时长. 首先,确保你已经在你的项目中添加了一个Button和一个Label控件.假 ...
- 如何在Linux云服务器上通过Docker Compose部署安装Halo,搭建个人博客网站?
目录 前置步骤 环境搭建 创建容器组 在系统任意位置创建一个文件夹 创建docker-compose.yaml 启动 Halo 服务 配置反向代理以及域名解析 Halo初始化页面. 更新新版本的hal ...