怎么样在MSSQL里生成文本的备份文件啊,就像mysql那样的!
我在一台笔记本上做开发,现将数据库的部分表结构和记录放到服务器中,不知怎么操作了!
MSSQL有没有生成文本的sql语句方式备份数据的方法啊??就像在mysql上的哪样生成如
insert into * ………………
急啊,今天下午就得发布了!!救救我啊!!分没了,有的话会多给的!
==================
有的,到企业管理器找到要生成脚本的表(一次可以多选),点击右键--所有任务--生成SQL脚本
===================
但是此脚本不含记录啊,我怎么才能让表中的记录也随之备份出来呢??
帮帮我啊老大!!!
===================
没有,只有自己写脚本实现,比如按上方法先建好表,数据可以通过导入导出实现.
如果你是要整个数据库,那么可以用备份还原实现
===================
-- 根据表中数据生成insert语句的存储过程
Create Proc proc_insert (@tablename varchar(256))
as -- 表名称
begin
set nocount on
Declare @sqlstr varchar(4000),
@sqlstr1 varchar(4000),
@sqlstr2 varchar(4000)
Select @sqlstr='select ''Insert '+@tablename
Select @sqlstr1= ' Values ( ''+', @sqlstr2=' ('
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 =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
exec( @sqlstr)
set nocount off
end
GO
-- 调用
exec proc_insert '表名'
=========================
表结构就象一楼那样生成SQL脚本
==========================
新建存储过程--文本中输入您给的大段代码(没有调用那行,只到GO)---确定
提示:数据库中存在名为'proc_insert'的对象!!
============================
难道楼主不可以数据库导出来,然后在服务器上恢复,
然后删除部分表,再选择性的删除表中部分记录???
===========================
提示:数据库中存在名为'proc_insert'的对象!!
--------------
该存储过程的名字 就可以了
然后调用时用你改后的哪个名字
===========================
把数据导出成xls或是两台机器复制,表的结构,主键,默认值都有所变化,不知是不是我操作不对。
提示:数据库中存在名为'proc_insert'的对象!!
--------------
该存储过程的名字 就可以了
然后调用时用你改后的哪个名字
----------
存储过程的名字还没有写呢啊?
还有怎么调用啊,是不是在查询分析器里执行:exec proc_insert '表名'??
========================
还有你的这里用改什么吗??我是不是得把表的名称写在这里呢??
Create Proc proc_insert (@tablename varchar(256))
as -- 表名称
==========================
谢谢lsqkeke(可可) !已经执行,这个问题解决拉,但又有了新的问题,,,
1.当我表的第一的字段为索引的时候,我用这个存储过程生成的脚本中含有第一个字段,所以我在执行生成的insert的时候会出错:
当 IDENTITY_INSERT 设置为 OFF 时,不能向表 'T_work_rep' 中的标识列插入显式值。
2.当我的表的字段非常多时候,这个存储过程读不到我所有的字段!!
改那个参数可以让读取的字段数更多呢??
3.当我字段中的数据较长的时候,不能完整读取该字段的所有数据,这个和第二个问题一样啊,改那个变量呢??
楼下读帖的顶一下吧,我说话三次了,要被禁言了!!
===================
直接用backup 数据库不是更好吗??
要导出结构语句就要手动生成了
===================
谁能将lsqkeke(可可)的存储过程改一下呢?满足以下条件的,谢谢啊!!要不谁推荐我一本sql语法的书,我自己学学看!
1.当我表的第一的字段为索引的时候,我用这个存储过程生成的脚本中含有第一个字段,所以我在执行生成的insert的时候会出错:
当 IDENTITY_INSERT 设置为 OFF 时,不能向表 'T_work_rep' 中的标识列插入显式值。
2.当我的表的字段非常多时候,这个存储过程读不到我所有的字段!!
改那个参数可以让读取的字段数更多呢??
3.当我字段中的数据较长的时候,不能完整读取该字段的所有数据,这个和第二个问题一样啊,改那个变量呢??
没有评论:
发表评论