SQL Server数据库导出包含数据的SQL脚本的方法
沉沙 2018-05-09 来源 : 阅读 1314 评论 0

摘要:通常情况下,SQL Server里面的生成SQL脚本,只会包含数据库及表的字段结构,而不会包含表的数据。本文主要讲解如何在SQL Server数据库里导出包含数据的SQL脚本,希望读后此文对你学习SQL Server数据库的知识点有更加清晰的理解和运用。

通常情况下,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 ( @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 你的表名 

选择【执行模式】为“以文本显示结果”,然后将运行后的结果存成.sql,加上用SQL Server生成的数据库脚本就可以了。

另外可以利用第三方工具,导出数据可以用powerbuilder。在database painter里面,用SQL选出,或者直接打开表,点击生成的list datawindow,然后在菜单file->save rows as->选择SQL,那么生成的SQL语句就包括建表和insert数据的SQL了。


本文由 @沉沙 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程