使用Sql Server Management Studio 2008将数据导出到Sql文件中

最近需要将一个Sql Server 2005数据库中的数据导出,为了方便,就希望能导出成Sql文件,里面包含的数据是由Insert 语句组成的。

在Sql Server Management Studio 2005 中(包括之前Sql Server2000 的企业管理器),只能把表结构导出到Sql脚本文件中,没办法把数据导出到Sql脚本文件中。

Sql Server  Management Studio 2008中增加了这个功能,因此我们可以利用2008的Management Studio工具连接2005的数据库,从而导出想要的脚本。

1.使用Sql Server Management Studio 2008 连接数据库。

2.选中要导出数据的数据库节点,点鼠标右键,在菜单中选择“任务”->“生成脚本”,如图:

selectMenu

3。在弹出的界面中,点2次“下一步”进入如图界面中,把“编写数据的脚步”置为true。

SetGenerateDataScriptToTrue

4。其它的操作点“下一步”即可。下图是最后生成的Sql脚本:

DataInsertSqlScript

注意:这里一定要使用Sql Server Management  Studio 2008,其它版本不行。

有问题欢迎大家留言。

[转自]http://www.cnblogs.com/greenerycn/archive/2010/04/09/SqlServer2008GenerateDataScript.html

附上一份非Sql server 导出数据的Sp

 1:   
 2:  create PROCEDURE [dbo].[UspOutputData]    
 3:  @tablename sysname    
 4:  AS    
 5:  declare @column varchar(2000)    
 6:  declare @columndata varchar(2000)    
 7:  declare @sql varchar(8000)    
 8:  declare @xtype tinyint    
 9:  declare @name sysname    
10:  declare @objectId int    
11:  declare @objectname sysname    
12:  declare @ident int    
13:  set nocount on    
14:  -- 判?斷?對?象?是?否?存?在?   
15:  set @objectId=object_id(@tablename)    
16:  if @objectId is null    
17:  begin    
18:  print 'The object not exists'    
19:  return    
20:  end    
22:  set @objectname=rtrim(object_name(@objectId))    
23:  if @objectname is null or charindex(@objectname,@tablename)=0    
24:  begin    
25:  print 'object not in current database'    
26:  return    
27:  end    
28:  -- 判?斷?對?象?是?否?是?table    
29:  if OBJECTPROPERTY(@objectId,'IsTable') < > 1    
30:  begin    
31:  print 'The object is not table'    
32:  return    
33:  end    
35:  select @ident=status&0x80 from syscolumns where id=@objectid and status&0x80=0x80    
36:  if @ident is not null    
37:  print 'SET IDENTITY_INSERT '+@TableName+' ON'    
38:    
39:  declare syscolumns_cursor cursor   
40:  for select c.name,c.xtype from syscolumns c where c.id=@objectid order by c.colid    
41:  open syscolumns_cursor    
42:  set @column=''    
43:  set @columndata=''    
44:  fetch next from syscolumns_cursor into @name,@xtype    
45:  while @@fetch_status < >-1    
46:    begin    
47:      if @@fetch_status < >-2    
48:        begin    
50:            begin    
51:              set @column=@column+case when len(@column)=0 then'' else ','end+@name     
52:              set @columndata=@columndata+case when len(@columndata)=0 then '' else ','','','end    
53:                  +case when @xtype in(167,175) then '''''''''+'+@name+'+''''''''' --varchar,char    
54:                        when @xtype in(231,239) then '''N''''''+'+@name+'+''''''''' --nvarchar,nchar    
55:                        when @xtype=61 then '''''''''+convert(char(23),'+@name+',121)+''''''''' --datetime    
56:                        when @xtype=58 then '''''''''+convert(char(16),'+@name+',120)+''''''''' --smalldatetime    
57:                        when @xtype=36 then '''''''''+convert(char(36),'+@name+')+''''''''' --uniqueidentifier    
58:                        else @name end    
59:            end    
60:        end    
61:      fetch next from syscolumns_cursor into @name,@xtype    
62:    end    
63:  close syscolumns_cursor    
64:  deallocate syscolumns_cursor    
65:  set @sql='set nocount on select ''insert '+@tablename+'('+@column+') values(''as ''--'','+@columndata+','')'' from '+@tablename    
66:  print '--'+@sql    
67:  exec(@sql)     
69:  if @ident is not null    
70:  print 'SET IDENTITY_INSERT '+@TableName+' OFF'    
71:  
72:  GO
73:  
74:  
75:  
原文地址:https://www.cnblogs.com/mmmhhhlll/p/1711381.html