时间:2021-07-01 10:21:17 帮助过:18人阅读
- -- To allow advanced options to be changed.
- exec sys.sp_configure ‘show advanced options‘, 1;
- go
- -- To update the currently configured value for advanced options.
- reconfigure;
- go
使用以下代码启用xp_cmdshell选项:
- -- To enable the feature.
- exec sys.sp_configure ‘xp_cmdshell‘, 1;
- go
- -- To update the currently configured value for this feature.
- reconfigure;
- go
使用以下代码禁用xp_cmdshell选项:
- -- To disable the feature.
- exec sys.sp_configure ‘xp_cmdshell‘, 0;
- go
- -- To update the currently configured value for this feature.
- reconfigure;
- go
2,常用的DOS命令
该存储过程使得用户可以通过TSQL命令执行DOS命令,参数是命令字符串:
- exec sys.xp_cmdshell ‘command_string‘
2.1 建立新文件或增加文件内容
格式:ECHO 文件内容>file_name
- exec master.dbo.xp_cmdshell ‘echo abc > D:\share\test.txt‘
2.2 查看文件内容
格式:TYPE file_name
- exec master.dbo.xp_cmdshell ‘type D:\share\test.txt‘
2.3 复制文件
格式: COPY file_name new_folder
- exec master.dbo.xp_cmdshell ‘copy D:\test\test.txt D:\share\‘
2.4 显示目录
格式:DIR folder
- exec master.dbo.xp_cmdshell ‘dir D:\share\‘
2.5 创建目录
格式:MD folder_name
- exec master.dbo.xp_cmdshell ‘md D:\share\test\‘
2.6 删除目录
格式:RD folder
- exec master.dbo.xp_cmdshell ‘rd D:\share\test‘
2.7 删除文件
格式:DEL file_name
- exec master.dbo.xp_cmdshell ‘del D:\share\test.txt‘
2.8 重命名文件
格式:REN [盘符:][路径]〈旧文件名〉〈新文件名〉
- exec master.dbo.xp_cmdshell ‘ren D:\test\test.txt new.txt‘
2.9 移动文件
格式:MOVE file_name new_folder
- exec master.dbo.xp_cmdshell ‘move D:\test\new.txt D:\share\‘
2.10 切换目录
格式:CD[盘符:][路径名][子目录名]
3,执行BCP命令
参考文档:
xp_cmdshell Server Configuration Option
xp_cmdshell (Transact-SQL)
常见dos命令总结
DOS中的ECHO命令详解
SQL Server 文件操作
标签:nis 建议 路径 add 操作系统 format 行数据 folder bcp