日期:2014-05-18  浏览次数:20451 次

#####怎样通过命令来备份sqlserver数据库呢?#####
数据库是sqlserver2000,我想要的是通过命令来备份一个数据库,但是不想要表里的数据,只要数据库结构(表,存储过程,函数等),导出到文本文档也是可以的,呵呵,不知道我说清楚了没有?backup   database的命令好像不行的,bcp命令也没有找到。如果您也遇到过类似的问题,能分享一下经验吗?

------解决方案--------------------
MSSQL生成整个数据库的SQL脚本的工具
scptxfr.exe的路径要正确
declare @cMd varchar(1000)
set @cmd = 'master.dbo.xp_cmdshell ' +
' ' 'c:\ "Microsoft ' +
'SQL Server " ' +
'\MSSQL\Upgrade\scptxfr.exe ' +
' /s YourServerName /p YourSAPassword /I /d YourDBName /f ' +
'c:\YourDBName.sql ' ' '
exec (@cmd)

工具参数说明:
SCPTXFR /s <server> /d <database> {[/I] | [/P <password> ]}
{[/F <script files directory> ] | [/f <single script file> ]}
/q /r /O /T /A /E /C <CodePage> /N /X /H /G /Y /?
/s - Indicates the source server to connect to.
/d - Indicates the source database to script.
/I - Use integrated security.
/P - Password to use for 'sa '. Note that login ID is always 'sa '.
If /P not used or if a password does not follow the flag,
a null password is used. Not compatible with /I.
/F - The directory into which the script files should be generated.
This means one file is generated for each category of objects.
/f - The single file in which all script is to be saved.
Not compatible with /F.
/q - Use quoted identifiers in the generated scripts.
/r - Include drop statements for the objects in the script.
/O - Generate OEM script files. Cannot be used with /A or /T.
This is the default behavior.
/T - Generate UNICODE script files. Cannot be used with /A or /O.
/A - Generate ANSI script files. Cannot be used with /T or /O.
/? - Command line help.
/E - Stop scripting when error occurs.
Default behavior is to log the error, and continue.
/C - Indicate the CodePage which overrides the server CodePage.
/N - Generate ANSI PADDING.
/X - Script SPs and XPs to separate files.
/H - Generate script files without header (default: with header).
/G - Use the specified server name as the prefix for the generated
output files(to handle dashes in server name).
/Y - Generate script for Extended Properties (valid for 8.x server
only).

------解决方案--------------------
up up
------解决方案--------------------
/*
在查询分析器中调用sqldmo生成脚本--存储过程

邹建 2003.07(引用请保留此信息)--*/

/*--调用实例
declare @str varchar(8000)
exec sp_getscript 'zj ', ' ', ' ', 'xzkh_sa ', '地区资料 ',@str output
print @str
*/
if exists(select 1 from sysobjects where id=object_id( 'sp_getscript ') and objectproperty(id, 'IsProcedure ')=1)
drop procedure sp_getscript
go
create procedure sp_getscript
@servername varchar(50) --服务器名
,@userid varchar(50) --用户名,如果为nt验证方式,则为空
,@password varchar(50) --密码
,@databasename varchar(50) --数据库名称
,@objectname varchar(250) --对象名
,@re varchar(8000) output --返回脚本
as
declare @srvid int,@dbsid int --定义服务器、数据库集id
declare @dbid int,@tbid int --数据库、表id
declare @err int,@src varchar(255), @desc varchar(255) --错误处理变量

--创建sqldmo对象
exec @err=sp_oacreate 'sqldmo.sqlserver ',@srvid output
if @err <> 0 goto lberr

--连接服务器
if isnull(@userid, ' ')= ' ' --如果是 Nt验证方式
begin
exec @err=sp_oasetproperty @srvid, 'loginsecure ',-1
if @err <> 0 goto lberr