关于生成某个表的脚本(带表中数据)-【50分】

敦厚的曹操 2012-11-24 05:33:08
我用的是Sql 2008,我想把某一个表(不是数据库中的所有表)连同表数据生成一个脚本。
【右键点击表】 --> 【编写表脚本为】 --> 【CREATE到】 --> 【新查询编辑器窗口】,但得到的是数据表的结构脚本,无法得到连同数据的脚本。
看在结贴率的份上,愿出50分指教,谢谢了!
...全文
322 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
敦厚的曹操 2012-11-30
  • 打赏
  • 举报
回复
谢谢,各位,马上结贴! 楼上的,我说的是单独一个表及数据一块导出脚本,不是数据库,看来微软没有此项功能!
發糞塗牆 2012-11-28
  • 打赏
  • 举报
回复

ALTER proc [dbo].[sp_CreateInsertScript] (@tablename varchar(256),@con nvarchar(400)) 
as 
begin 
set nocount on 
declare @sqlstr varchar(4000) 
declare @sqlstr1 varchar(4000) 
declare @sqlstr2 varchar(4000) 
select @sqlstr='select ''insert '+@tablename 
select @sqlstr1='' 
select @sqlstr2='(' 
select @sqlstr1='values (''+' 
select @sqlstr1=@sqlstr1+col+'+'',''+' ,@sqlstr2=@sqlstr2+name +',' from (select case 
when a.xtype =173 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.length*2+2)+'),'+a.name +')'+' end' 
when a.xtype =104 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(1),'+a.name +')'+' end' 
when a.xtype =175 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end' 
when a.xtype =61 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'convert(varchar(23),'+a.name +',121)'+ '+'''''''''+' end' 
when a.xtype =106 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.xprec+2)+'),'+a.name +')'+' end' 
when a.xtype =62 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(23),'+a.name +',2)'+' end' 
when a.xtype =56 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(11),'+a.name +')'+' end' 
when a.xtype =60 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(22),'+a.name +')'+' end' 
when a.xtype =239 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end' 
when a.xtype =108 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.xprec+2)+'),'+a.name +')'+' end' 
when a.xtype =231 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end' 
when a.xtype =59 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(23),'+a.name +',2)'+' end' 
when a.xtype =58 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'convert(varchar(23),'+a.name +',121)'+ '+'''''''''+' end' 
when a.xtype =52 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(12),'+a.name +')'+' end' 
when a.xtype =122 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(22),'+a.name +')'+' end' 
when a.xtype =127 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(6),'+a.name +')'+' end' 
when a.xtype =48 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(6),'+a.name +')'+' end' 
when a.xtype =165 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.length*2+2)+'),'+a.name +')'+' end' 
when a.xtype =167 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end' 
else '''NULL''' 
end as col,a.colid,a.name 
from syscolumns a where a.id = object_id(@tablename) 
and a.xtype <>189 and a.xtype <>34 and a.xtype <>35 and a.xtype <>36 
)t order by colid 
select @sqlstr=@sqlstr+left(@sqlstr2,len(@sqlstr2)-1)+') '+left(@sqlstr1,len(@sqlstr1)-3)+')'' from '+@tablename + ' where 1=1 and ' + isnull(@con,'') 
print @sqlstr 
exec( @sqlstr) 
set nocount off 
end
敦厚的曹操 2012-11-28
  • 打赏
  • 举报
回复
既然数据库生成脚本可以带数据生成,为什么表生成就不能带数据生成,哎!!! 看来还得期待sql更高的版本了。
zlp321002 2012-11-28
  • 打赏
  • 举报
回复
生成的时候可以选择架构和数据
zlp321002 2012-11-28
  • 打赏
  • 举报
回复
不是可以生成架构和脚本吗?
七色鸟 2012-11-28
  • 打赏
  • 举报
回复
动软啊!很方便......
Viccy_Yao 2012-11-28
  • 打赏
  • 举报
回复
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 你的表名 或者用动软代码生成器
發糞塗牆 2012-11-28
  • 打赏
  • 举报
回复
奇怪,我这里网络卡还是怎么样,刷几十次才看到你回复
發糞塗牆 2012-11-28
  • 打赏
  • 举报
回复
第一个参数:表名, 第二个参数:条件名,如果整表导出来就传'' 例子: exec [sp_CreateInsertScript] 'tbname','dictypeid = 61'
99归一 2012-11-28
  • 打赏
  • 举报
回复
引用 7 楼 DBA_Huangzj 的回复:
SQL code?123456789101112131415161718192021222324252627282930313233343536373839404142ALTER proc [dbo].[sp_CreateInsertScript] (@tablename varchar(256),@con nvarchar(400)) as begin set noco……
黄哥的我试过了,可以的,原帖的地址在这http://bbs.csdn.net/topics/390282055,楼主可以自己做个测试表试试
敦厚的曹操 2012-11-28
  • 打赏
  • 举报
回复
引用 7 楼 DBA_Huangzj 的回复:
SQL code?123456789101112131415161718192021222324252627282930313233343536373839404142ALTER proc [dbo].[sp_CreateInsertScript] (@tablename varchar(256),@con nvarchar(400)) as begin set noco……
谢谢了,知道是存储过程,也有参数,水平低,看不太懂,必须输入连接是吧, 能不能给例子或说明,主要是调用,谢了!
黄_瓜 2012-11-28
  • 打赏
  • 举报
回复
木有办法。ms 没做这个功能。
-Tracy-McGrady- 2012-11-28
  • 打赏
  • 举报
回复
dodolzc10 2012-11-24
  • 打赏
  • 举报
回复
如果要经常做这样的操作的话,就用Toad for SQL Server 4.6 Trial吧! 我就用这个,因为技术不咋的,不敢跨服务器更新数据
敦厚的曹操 2012-11-24
  • 打赏
  • 举报
回复
其实我的目的是, 把一个表完全复制(连同表结构和表数据)到另一个表中的最简单的方法,这边一创建脚本,那边一执行,就创建了相同的表。 不用insert命令,这个太麻烦,因为设计到数据库连接。
敦厚的曹操 2012-11-24
  • 打赏
  • 举报
回复
楼上的,您好,谢谢答复! 不过,我说的是单独一个表,不是数据库生成脚本,数据库右键生成脚本这个我会。 我说的是单独一个表,生成创建形式的表和数据的脚本。 继续等高手.......
ForFumm 2012-11-24
  • 打赏
  • 举报
回复
2008可以的,在【选择脚本项】时,把编写数据脚本 设置为:true就可以了。
极品老土豆 2012-11-24
  • 打赏
  • 举报
回复
第一,bcp 可以导出数据,然后再用bcp导入 第二,自动生成的表结构脚本,可能无法使用bcp导入 举个例子,你最初创建表时候,是B,C,X,D,E,A 那么你自动生成表结构标准后,可能会这样 A,B,C,D,E,X 导致 无法识别数据 以上字母为字段的替代

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧