将表数据生成SQL脚本的存储过程和工具
作者:大海 发布时间:2009-01-08 15:24:00
将表数据生成SQL脚本的存储过程示例:
CREATE PROCEDURE dbo.UspOutputData @tablename sysname
AS
declare @column varchar(1000)
declare @columndata varchar(1000)
declare @sql varchar(4000)
declare @xtype tinyint
declare @name sysname
declare @objectId int
declare @objectname sysname
declare @ident int
set nocount on
set @objectId=object_id(@tablename)
if @objectId is null -- 判断对象是否存在
begin
print 'The object not exists'
return
end
set @objectname=rtrim(object_name(@objectId))
if @objectname is null or charindex(@objectname,@tablename)=0 --此判断不严密
begin
print 'object not in current database'
return
end
if OBJECTPROPERTY(@objectId,'IsTable') < > 1 -- 判断对象是否是table
begin
print 'The object is not table'
return
end
select @ident=status&0x80 from syscolumns where id=@objectid and status&0x80=0x80
if @ident is not null
print 'SET IDENTITY_INSERT '+@TableName+' ON'
declare syscolumns_cursor cursor
for select c.name,c.xtype from syscolumns c where c.id=@objectid order by c.colid
open syscolumns_cursor
set @column=''
set @columndata=''
fetch next from syscolumns_cursor into @name,@xtype
while @@fetch_status < >-1
begin
if @@fetch_status < >-2
begin
if @xtype not in(189,34,35,99,98) --timestamp不需处理,image,text,ntext,sql_variant 暂时不处理
begin
set @column=@column+case when len(@column)=0 then'' else ','end+@name
set @columndata=@columndata+case when len(@columndata)=0 then '' else ','','','
end
+case when @xtype in(167,175) then '''''''''+'+@name+'+''''''''' --varchar,char
when @xtype in(231,239) then '''N''''''+'+@name+'+''''''''' --nvarchar,nchar
when @xtype=61 then '''''''''+convert(char(23),'+@name+',121)+''''''''' --datetime
when @xtype=58 then '''''''''+convert(char(16),'+@name+',120)+''''''''' --smalldatetime
when @xtype=36 then '''''''''+convert(char(36),'+@name+')+''''''''' --uniqueidentifier
else @name end
end
end
fetch next from syscolumns_cursor into @name,@xtype
end
close syscolumns_cursor
deallocate syscolumns_cursor
set @sql='set nocount on select ''insert '+@tablename+'('+@column+') values(''as ''--'','+@columndata+','')'' from '+@tablename
print '--'+@sql
exec(@sql)
if @ident is not null
print 'SET IDENTITY_INSERT '+@TableName+' OFF'
GO
exec UspOutputData 你的表名
工具介绍:
1 InstallShield自带一个把SQL数据库的结构和数据导出为脚本的功能,很好用,如果刚好要做打包,建议用这个,因为安装文件比较大,如果不是做打包不建议用。
2 SqlInsert是一个专门导出数据为脚本的小工具,速度没IS的快,但是一样好用。
猜你喜欢
- watch监听对象里面值的变化后台管理有时候有选择选择框的不同选项,会影响下一个选择框的内容,如下图 .这个时候就可以
- 最近接触到Tencent的广告越来越多,户外广告牌和电视上都是,可以明显感觉到Tencent的扩张野心,以及对QQ品牌定位的重塑。摆脱低龄化
- 导言:在前面的3章里我们为处理二进制数据添加了很多的功能。我们首先在表Categories里添加BrochurePath列,并更新了体系结构
- 本文实例为大家分享了Django实现文件上传下载的具体代码,供大家参考,具体内容如下一、django实现文件下载(1)、后台接口如果从服务器
- 1.对于一维数组,可以有:2. 对于二维数组:考虑可将其看作为矩阵,故可以如下书写二重遍历 这里外层循环的是二维数组A的行,内层则
- 我就废话不多说了,大家还是直接看代码吧!绘制曲线:import timeimport numpy as npimport matplotli
- 1. 题目编写程序, 4名牌手打牌,计算机随机将52张牌(不含大小鬼)发给4名牌手,在屏幕上显示每位牌手的牌。提示:设计出3个类:Card类
- 本文实例讲述了Yii2框架实现登陆添加验证码功能。分享给大家供大家参考,具体如下:models中LoginForm.phppublic $v
- 刚开始使用django,在创建第一个app时被提示不知道命令runserver,百度得出是环境变量的问题。1、配置python变量环境,C:
- 如下所示:#求一个数的最大约数(不算本身)def getmaxnum(n): num = n //2 while n
- 输入汉字提示拼音,试试下面这个函数,不知是不是你要的那个:查询汉字便宜到词典网<%function getpychar(ch
- var obj=document.getElementById("id");得到的是dom对象,对该对象进行操作的时候使
- 经常会有定时备份SQL Server数据库的需要。定时备份到本机的话,还是挺容易的,计划任务就可以完成,但如果是整机挂了,那备份到本机是没意
- 本文实例讲述了Python数组定义方法。分享给大家供大家参考,具体如下:Python中没有数组的数据结构,但列表很像数组,如:a=[0,1,
- python爬虫模块selenium简介selenium主要是用来做自动化测试,支持多种浏览器,爬虫中主要用来解决JavaScript渲染问
- 挂载全局方法使用jsencrypt进行rsa加密原文链接:Js参数RSA加密传输,jsencrypt.js的使用 *https://www.
- 一、图的定义图是一种比树更复杂的一种数据结构,在图结构中,结点之间的关系是任意的,任意两个元素之间都可能相关,因此,它的应用极广。图中的数据
- 通过前面内容的介绍,我们对 Surface 对象有了大体上的认识。Pygame 针对文本、图像、颜色提供了不同模块来生成它们各自的 Surf
- 前言简单介绍下python的几个自动求导工具,tangent、autograd、sympy;在各种机器学习、深度学习框架中都包含了自动微分,
- 需求:在刷word题库的时候,答案就在题目下方,干扰复习效果,将答案字体变成白色,查看答案的时候只需要将答案背景刷黑转换需求:在word中找