use master
go
if exists (select name from sysobjects where name = 'sp_generate_insert_script')
begin
drop proc sp_generate_insert_script
print 'old version of sp_generate_insert_script dropped'
end
go
create procedure sp_generate_insert_script
@tablename_mask varchar(30) = NULL
as
begin
declare @tablename varchar (128)
declare @tablename_max varchar (128)
declare @tableid int
declare @columncount numeric (7,0)
declare @columncount_max numeric (7,0)
declare @columnname varchar (30)
declare @columntype int
declare @string varchar (30)
declare @leftpart varchar (8000)
declare @rightpart varchar (8000)
declare @hasident int
set nocount on
-- take ALL tables when no mask is given (!)
if (@tablename_mask is NULL)
begin
select @tablename_mask = '%'
end
-- create table columninfo now, because it will be used several times
create table #columninfo
(num numeric (7,0) identity,
name varchar(30),
usertype smallint) select name,
id
into #tablenames
from sysobjects
where type in ('U' ,'S')
and name like @tablename_mask
-- loop through the table #tablenames
select @tablename_max = MAX (name),
@tablename = MIN (name)
from #tablenames
while @tablename <= @tablename_max
begin
select @tableid = id
from #tablenames
where name = @tablename
if (@@rowcount <> 0)
begin
-- Find out whether the table contains an identity column
select @hasident = max( status & 0x80 )
from syscolumns
where id = @tableid
truncate table #columninfo
insert into #columninfo (name,usertype)
select name, type
from syscolumns C
where id = @tableid
and type <> 37 -- do not include timestamps
-- Fill @leftpart with the first part of the desired insert-statement, with the fieldnames
select @leftpart = 'select ''insert into '+@tablename
select @leftpart = @leftpart + '('
select @columncount = MIN (num),
@columncount_max = MAX (num)
from #columninfo
while @columncount <= @columncount_max
begin
select @columnname = name,
@columntype = usertype
from #columninfo
where num = @columncount
if (@@rowcount <> 0)
begin
if (@columncount < @columncount_max)
begin
select @leftpart = @leftpart + @columnname + ','
end
else
begin
select @leftpart = @leftpart + @columnname + ')'
end
end
select @columncount = @columncount + 1
end
select @leftpart = @leftpart + ' values('''
-- Now fill @rightpart with the statement to retrieve the values of the fields, correctly formatted
select @columncount = MIN (num),
@columncount_max = MAX (num)
from #columninfo
select @rightpart = ''
while @columncount <= @columncount_max
begin
select @columnname = name,
@columntype = usertype
from #columninfo
where num = @columncount
if (@@rowcount <> 0)
begin
if @columntype in (39,47)
begin
select @rightpart = @rightpart + '+'
select @rightpart = @rightpart + 'ISNULL(' + replicate( char(39), 4 ) + '+replace(' + @columnname + ',' +
replicate( char(39), 4 ) + ',' + replicate( char(39), 6) + ')+' + replicate( char(39), 4 ) + ',''NULL'')'
end
else if @columntype = 35 begin
select @rightpart = @rightpart + '+'
select @rightpart = @rightpart + 'ISNULL(' + replicate( char(39), 4 ) + '+replace(convert(varchar(1000),' +
@columnname + ')' + ',' + replicate( char(39), 4 ) + ',' + replicate( char(39), 6 ) + ')+' + replicate( char(39), 4 ) +
',''NULL'')'
end
else if @columntype in (58,61,111)
begin
select @rightpart = @rightpart + '+'
select @rightpart = @rightpart + 'ISNULL(' + replicate( char(39), 4 ) + '+convert(varchar(20),' + @columnname +
')+'+ replicate( char(39), 4 ) + ',''NULL'')'
end
else
begin
select @rightpart = @rightpart + '+'
select @rightpart = @rightpart + 'ISNULL(convert(varchar(99),' + @columnname + '),''NULL'')'
end if ( @columncount < @columncount_max)
begin
select @rightpart = @rightpart + '+'','''
end
end
select @columncount = @columncount + 1
end
end
select @rightpart = @rightpart + '+'')''' + ' from ' + @tablename
-- Order the select-statements by the first column so you have the same order for
-- different database (easy for comparisons between databases with different creation orders)
select @rightpart = @rightpart + ' order by 1'
-- For tables which contain an identity column we turn identity_insert on
-- so we get exactly the same content
if @hasident > 0
select 'SET IDENTITY_INSERT ' + @tablename + ' ON'
exec ( @leftpart + @rightpart )
if @hasident > 0
select 'SET IDENTITY_INSERT ' + @tablename + ' OFF'
select @tablename = MIN (name)
from #tablenames
where name > @tablename
end
end

再选择要导出语句的数据库,在“查询”中选择“结果保存为文件……”,执行EXEC sp_generate_insert_script '表名',如果不写表名,将

导出数据库中所有的表的内容。保存的文件即为Insert的SQL语句。

MSSQL中把表中的数据导出成Insert的更多相关文章

  1. 在SQL SERVER中获取表中的第二条数据

    在SQL SERVER中获取表中的第二条数据, 思路:先根据时间逆排序取出前2条数据作为一个临时表,再按顺时排序在临时表中取出第一条数据 sql语句如下: select top 1 * from(se ...

  2. 查看hive中某个表中的数据、表结构及所在路径

    查看hive中action_data_myisam表中的数据.表结构及所在路径 1.客户端进入hive环境:hive 2.查看表数据,鉴于数据量大,这里只显示前五条:select * from act ...

  3. 在Action中获取表单提交数据

    -----------------siwuxie095 在 Action 中获取表单提交数据 1.之前的 Web 阶段是提交表单到 Servlet,在其中使用 Request 对象 的方法获取数据 2 ...

  4. 在sqlServer中把数据导出为insert脚本

    有时候为了把数据导出为insert脚本,不得不用一些小工具,或者通过自己写存储过程来完成这一操作.其实SqlServer本身就有这种功能.以下是详细步骤:

  5. 备忘:MySQL中修改表中某列的数据类型、删除外键约束

    -- MySQL中修改表中某列的数据类型 ALTER TABLE [COLUMN] 表名 MODIFY 列名 列定义; -- 删除外键约束 SHOW CREATE TABLE 表名; -- 复制CON ...

  6. Pl/sql 如何将oracle的表数据导出成excel文件?

    oracle将表数据导出成excel文件的方法 1)在SQL窗体上,查询需要导出的数据 --查询数据条件-- ; 结果视图 2)在查询结果的空白处,右键选择Copy to Excel 3) 查看导出e ...

  7. 把mysql的数据导出成txt

    把mysql的数据导出成txt select a from b into outfile '/sqlfile/a.txt'; my.ini里需要设置secure_file_priv = d:/sqlf ...

  8. sqlserver数据库导出成insert语句

    点击数据库名称右键=========>任务========>生成脚本 一.表结构导出成sql语句 二.数据导出成sql语句

  9. 【SQL Sever】将SQL Sever中的一个数据表的数据导出为insert语句

    例如:这SQL   Sever中的一张数据表,想要将这张数据表中的数据  转化成一个一个的insert语句存储在txt的文档中,那么不论走到那里这个insert语句一执行,我们就能将这个数据表中的数据 ...

随机推荐

  1. asp.net中的mysql传参数MySqlParameter

    注意在asp.net中传参 string sql="select name,id from user where id=@id"; //@idm不需要引号 MySqlParamet ...

  2. android studio 中设置apk的版本号

    今天在mainfest.xml中设置版本号为2,(代码获取到的版本号无效) android:versionCode="2" android:versionName="2. ...

  3. 第三章DOM

    1. DOM的概念 D:Document. O:Object.对象可以分为三类, 1. 用户自定义的对象. 2. 内建对象,如Array,Math,Date. 3. 宿主对象,浏览器提供的对象.如wi ...

  4. [转]谈谈C++中的swap函数

    1,最通用的模板交换函数模式:创建临时对象,调用对象的赋值操作符. template <class T> void swap ( T& a, T& b ) { T c(a) ...

  5. Linux 命令整理 —— 用户管理

    Linux用户管理以读.写.执行动作为权限,以用户组为单位,限制用户行为.对于文件的的操作,可以限制读.写.执行中的哪一种,也可以限制文件所有者.组用户.组外用户相应的权限. 所以,要建立用户,最好先 ...

  6. python参考手册--第1章python简介

    1.if __name__ == '__main__': 直接运行myModel.py时,当前模块的名字是main:import myModel然后运行的时候当前模块的名字是myModel. 2.ev ...

  7. SPOJ MULTQ3 7299 Multiples of 3 (区间更新)

    题目连接:http://www.spoj.com/problems/MULTQ3/ #include <iostream> #include <stdio.h> #includ ...

  8. 核稀疏表示分类(KSRC)

    参考:<Kernel SparseRepresention-Based Classifier> 原文地址:http://www.cnblogs.com/Rosanna/p/3372153. ...

  9. **【ci框架】精通CodeIgniter框架

    http://blog.csdn.net/yanhui_wei/article/details/25803945 一.大纲 1.codeigniter框架的授课内容安排 2.codeigniter框架 ...

  10. hdu 4282 A very hard mathematic problem

    由于k的范围是0-2^31,而且x,y,z都是正整数,由题易知道2<=z<31,1<=x<y;所以直接枚举就好了!!! #include<iostream> #in ...