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语句,方便小批量转移数据的更多相关文章

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

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

  2. SQL Server 2012 数据库数据导出为脚本

    转自:http://blog.csdn.net/ituff/article/details/8265822 将高版本的的SQL Server数据库移到低版本的SQL Server是一件十分蛋疼的事,最 ...

  3. [转]实战 SQL Server 2008 数据库误删除数据的恢复

    实战 SQL Server 2008 数据库误删除数据的恢复 关键字:SQL Server 2008, recover deleted records 今天有个朋友很着急地打电话给我,他用delete ...

  4. SQL Server 2008 数据库误删除数据的恢复

    原文:SQL Server 2008 数据库误删除数据的恢复 原文:http://www.cnblogs.com/dudu/archive/2011/10/15/sql_server_recover_ ...

  5. sql server的数据库个数、表个数及表的数据量统计

    sql server的数据库个数.表个数及表的数据量统计   --由于今天要监控数据,急需统计实例中1有多少库2库里有多少表3每个表有多少数据 --将写好的代码贴出来,用到如下的: --sysobje ...

  6. Linux下使用FreeTDS访问MS SQL Server 2005数据库(包含C测试源码)

    Linux下使用FreeTDS访问MS SQL Server 2005数据库(包含C测试源码) http://blog.csdn.net/helonsy/article/details/7207497 ...

  7. c# datagridview与DataSet绑定, 列与数据库表里面的列一一对应

    参考代码1: 自己模拟出数据,并分别对dataGridView赋值. using System; using System.Collections.Generic; using System.Comp ...

  8. SQL server 2008数据库的备份与还原、分离(转)

    SQL server 2008数据库的备份与还原.分离(转)   一.SQL数据库的备份: 1.依次打开 开始菜单 → 程序 → Microsoft SQL Server 2008 → SQL Ser ...

  9. 微软ASP.NET网站部署指南(2):部署SQL Server Compact数据库

    1. 综述 对于数据库訪问,Contoso University程序要求以下的软件必须随程序一起部署.由于不属于.NET Framework: SQL Server Compact (数据库引擎) A ...

  10. Sql Server 2008R2 数据库发布与订阅

    背景描述: 发布服务器A: (远程端) , 数据库服务名: GUANWANG1 订阅服务器B: (本机)   ,  数据库服务名: PC-LLRDBA 需要从服务器A中数据库发布,然后在B中订阅A发布 ...

随机推荐

  1. 一个用来画拉氏图的简单Python脚本

    技术背景 关于拉氏图的更多介绍,可以参考下这篇博客,这里简单引述一部分内容: Ramachandran plot(拉氏图)是由G. N. Ramachandran等人于1963年开发的,用来描述蛋白质 ...

  2. SpringBoot集成ShardingSphere分表中间件

    ShardingSphere简介 ShardingSphere 由 JDBC.Proxy 和 Sidecar(规划中)这 3 款既能够独立部署,又支持混合部署配合使用的产品组成. 它们均提供标准化的基 ...

  3. C#进行图片压缩(对jpg压缩效果最好)

    直接上代码 1 public static class ImageCompress 2 { 3 /// <summary> 4 /// 图片压缩 5 /// </summary> ...

  4. (五)Redis 缓存异常、应对策略

    1.缓存和数据库不一致 只要我们使用 Redis 缓存,就必然会面对缓存和数据库间的一致性保证问题,这里的"一致性"包含了两种情况:缓存中有数据且与数据库中的值相同.缓存中没有数据 ...

  5. Vscode控制台乱码的最终解决方案

    Vscode控制台乱码的最终解决方案 vscode运行项目时控制台打印日志乱码.网上也有许多解决办法. 方法一[管用]推荐,避免过多设置 Java项目时,像Springboot微服务项目默认使用的是l ...

  6. PHP str_replace() 函数详解

    PHP str_replace() 函数详解 1.前言: str_replace() 函数以其他字符替换字符串中的一些字符(区分大小写). 该函数区分大小写.请使用 str_ireplace() 函数 ...

  7. Linux-makefile命令后面的-j4 -j8是什么意思?

    其实是指在编译指定的文件时用多少个线程进行编程的意思~ 相关命令示例如下: make zImage -j8 make modules -j8 --------------------------- m ...

  8. [oeasy]python0091_仙童公司_八叛逆_intel_8080_altair8800_牛郎星

    编码进化 个人电脑 计算机 通过电话网络 进行连接 极客 利用技术 做一些有趣的尝试 极客文化 是 认真研究技术的 文化 计算机 不再是 高校和研究机构高墙里面的 神秘事物 而是 生活中常见的 家用电 ...

  9. PyQt 右键菜单的实现(Qt.CustomContextMenu方式)

    从Qt文档Qt::ContextMenuPolicy的值可以看出,实现右键菜单的方式有三种,这对于所有继承于QWidget的类都是通用的,在用每一种方式实现之前都要调用QWidget::setCont ...

  10. 《Programming from the Ground Up》阅读笔记:p88-p94

    <Programming from the Ground Up>学习第5天,p88-p94总结,总计7页. 一.技术总结 1.touppercase.s #PURPOSE: This pr ...