SQL 中常用存储过程xp_cmdshell运行cmd命令 (转载)

目的:
使用SQL语句,在D盘创建一个文件夹myfile


首先查询系统配置

SELECT * FROM sys.configurations WHERE name='xp_cmdshell' OR name='show advanced options'
GO

可以看到他们的值为0,无法配置

打开系统配置:

USE master
GO
RECONFIGURE --先执行一次刷新,处理上次的配置
GO
EXEC sp_configure 'show advanced options',1 --启用xp_cmdshell的高级配置
GO
RECONFIGURE --刷新配置
GO
EXEC sp_configure 'xp_cmdshell',1  --打开xp_cmdshell,可以调用SQL系统之外的命令
GO
RECONFIGURE
GO
--使用xp_cmdshell在D盘创建一个myfile 文件夹
EXEC xp_cmdshell 'mkdir d:myfile',no_output --[no_output]表示是否输出信息
GO

可以看到在D盘创建了一个myfile 文件夹
执行第一句查看配置:

关闭系统配置:

--关闭
EXEC sp_configure 'show advanced options','1' --确保show advances options 的值为1,这样才可以执行xp_cmdshell为0的操作
GO
RECONFIGURE
GO
EXEC sp_configure 'xp_cmdshell',0 --关闭xp_cmdshell
GO
RECONFIGURE
GO
EXEC sp_configure 'show advanced options','0' --关闭show advanced options
GO
RECONFIGURE
GO

执行第一句:

原文链接

原文地址:https://www.cnblogs.com/OpenCoder/p/10239200.html