您的位置:首页 > 运维架构 > Shell

xp_cmdshell 拓展存储过程的使用

2010-11-03 13:46 459 查看
xp_cmdshell是常用的拓展存储过程,特可以完成DOS命令下的一些操作,诸如创建文件夹、列出文件列表等。

例如:希望创建的数据库保存在D:/project目录。如果当前没有此目录,使用create datebase语句创建时会报错,如何解决。我们可以使拓展存储过程来创建文件夹。

具体语法为:

exec xp_cmdshell DOS命令 [no_output]

--Purpose:xp_cmdshell 拓展存储过程的使用
use master
go

exec sp_configure 'show advanced options', 1;
go
reconfigure;
go
exec sp_configure
/*---创建数据库bankDB,要求保存在D:/bank---*/
exec xp_cmdshell 'mkdir d:/bank', NO_OUTPUT --创建文件夹D:/bank
--创建数据库bankDB
if exists(select * from sysdatabases where name='bankDB')
drop database bankDB
go
create database bankDB
on
(
name='bankDB_data',
filename='d:/bank/bankDB_data.mdf',
size=1mb,
filegrowth=10%
)
log on
(
name='bankDB_log',
filename='d:/bank/bankDB_log.ldf',
size=1mb,
filegrowth=10%
)
go
exec xp_cmdshell 'dir D:/bank/'
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: