SQL数据中运行cmd命令

在sql查询分析器里面是不能直接运行cmd命令的

但是SQL给出了一个接口

--打开高级设置
EXEC sp_configure 'show advanced options', 1
RECONFIGURE
--打开xp_cmdshell扩展存储过程
EXEC sp_configure 'xp_cmdshell', 1
RECONFIGURE

首先 打开一些配置

然后执行你要运行cmd命令

exec master..xp_cmdshell 'net start "computer browser"'
RECONFIGURE
--添加网络驱动器映射
exec master..xp_cmdshell 'net use z: //192.168.0.3/F$/DataBase "密码" /user:192.168.0.3(IP)/administrator(登陆用户名)

然后关闭相应的程序

--删除映射
exec master..xp_cmdshell 'net use z: /delete'
--关闭xp_cmdshell扩展存储过程、高级设置
EXEC sp_configure 'xp_cmdshell', 0
RECONFIGURE
EXEC sp_configure 'show advanced options', 0
RECONFIGURE

原文地址:https://www.cnblogs.com/dingdingmao/p/3146541.html