惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google Online Security Blog
Google Online Security Blog
D
Docker
人人都是产品经理
人人都是产品经理
Hugging Face - Blog
Hugging Face - Blog
腾讯CDC
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
宝玉的分享
宝玉的分享
Last Week in AI
Last Week in AI
L
LangChain Blog
月光博客
月光博客
U
Unit 42
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
GbyAI
GbyAI
Recent Announcements
Recent Announcements
MyScale Blog
MyScale Blog
N
Netflix TechBlog - Medium
D
DataBreaches.Net
T
Tailwind CSS Blog
H
Help Net Security
MongoDB | Blog
MongoDB | Blog
V
Visual Studio Blog
B
Blog
G
Google Developers Blog
有赞技术团队
有赞技术团队
Y
Y Combinator Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
云风的 BLOG
云风的 BLOG
Recorded Future
Recorded Future
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Google DeepMind News
Google DeepMind News
Jina AI
Jina AI
Engineering at Meta
Engineering at Meta
C
Check Point Blog
V
V2EX
爱范儿
爱范儿
Microsoft Azure Blog
Microsoft Azure Blog
T
The Blog of Author Tim Ferriss
博客园 - 聂微东
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
F
Full Disclosure
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
P
Proofpoint News Feed
罗磊的独立博客
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Google DeepMind News
Google DeepMind News
WordPress大学
WordPress大学
Apple Machine Learning Research
Apple Machine Learning Research
量子位
博客园 - 司徒正美
博客园 - 叶小钗

博客园 - BlackPhoenix

国内Asp.net博客系统收集和简单介绍 1999-2006中国统计年鉴 CodeSmith.Pro.4.1.2破解和注册 office2007的电脑上,右键菜单新建“office2003”文档的办法! 农行动态口令卡问题解决方案(Key:Vista,IE7,证书已锁定,438对象不支持此属性或方法) VS2005Team版如何进行单元测试 解决vista系统部分CHM打不开,“无法正常显示”的问题 序列化- 使用BinaryFormatter进行序列化 C#开源资源大汇总 Windows Vista中微软拼音输入法2007默认是英文标点问题解决 string.format()字符传格式化时特殊字符要进行转义 C#:String.Format数字格式化输出 ASP.NET-GridView的分页功能 [coll]ASP.NET刷新页面的六种方法 [转]DataFormatString的使用 [转]中国大学金融专业排名 SQL SERVER的字段类型说明及简单比较 两个月没编程了,现在开工! 结合委托与AJAX,实现无刷新确认对话框的开源用户控件
SQL Server将数据导出SQL脚本的方法
BlackPhoenix · 2009-08-21 · via 博客园 - BlackPhoenix

SQL Server里面的生成SQL脚本,只会包含数据库及表的字段结构,而不会包含表的数据,也就是SQL脚本里面只有Create database,Create table 这样的语句,没有insert into。那么我们怎么样才能导出数据呢?

SQL Server并不包含这个功能,只能靠第三方的代码了。

用这个存储过程可以实现:

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(@tablenameif @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,175then '''''''''+'+@name+'+''''''''' --varchar,char 
when @xtype in(231,239then '''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(@sqlif @ident is not null 
print 'SET IDENTITY_INSERT '+@TableName+' OFF' GO

使用方法为:

在查询分析器以“文本显示结果”方法执行
exec UspOutputData 你的表名
然后将运行后的结果存成.sql,加上用SQL Server生成的数据库脚本就可以了。

缺点和问题:

得到导出数据的语句,但image,text,ntext,sql_variant 列不出现在语句,以后改进。