设为首页收藏本站

EPS数据狗论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1761|回复: 0

SQL Server 文件操作

[复制链接]

30

主题

220

金钱

355

积分

入门用户

发表于 2019-3-26 16:21:47 | 显示全部楼层 |阅读模式
在master数据库中,SQL Server提供系统扩展的存储过程,其中有一些存储过程的命名以xp_开头,用于处理操作系统的文件。

一,判断文件是否存在
存储过程sys.xp_fileexist 用于判断文件是否存在,参数是文件(file)的路径或目录的路径:
  1. exec master.sys.xp_fileexist 'D:\test.txt'
复制代码

该存储过程返回的结果集有一行数据,三个字段,如下图:
628084-20180223111954280-1043410323.png

二,创建子目录
存储过程 sys.xp_create_subdir 用于创建子目录,参数是子目录的路径:
  1. exec master.sys.xp_create_subdir 'D:\test'
复制代码

执行存储过程,系统返回消息:Command(s) completed successfully,说明子目录创建成功。

三,查看子目录结构
存储过程sys.xp_dirtree 用于显示当前目录的子目录,该存储过程有三个参数:
directory:第一个参数是要查询的目录;
depth :第二个参数是要显示的子目录的深度,默认值是0,表示显示所有的子目录;
file :第三个参数是bool类型,指定是否显示子目录中的文件(file),默认值是0,表示不显示任何文件,只显示子目录(directory);
  1. exec master.sys.xp_dirtree 'D:\data'
复制代码

该存储过程返回的字段有子目录名称和相对深度,返回的结果中并没有显示子目录的父子关系:
628084-20180223113119738-1947767926.png

四,删除文件
存储过程 sys.xp_delete_file 用于删除文件,该存储过程有5个参数:
第一个参数是文件类型(File Type),有效值是0和1,0是指备份文件,1是指报表文件;
第二个参数是目录路径(Folder Path), 目录中的文件会被删除,目录路径必须以“\”结尾;
第三个参数是文件的扩展名(File Extension),常用的扩展名是'BAK' 或'TRN';
第四个参数是Date,早于该日期创建的文件将会被删除;
第五个参数是子目录(Subfolder),bool类型,0是指忽略子目录,1是指将会删除子目录中的文件;
该存储过程不会删除任意类型的文件,系统限制它只能删除特定类型(备份文件和报表文件)的文件。
  1. declare @Date datetime = dateadd(day,-30,getdate())
  2. exec master.sys.xp_delete_file 0,'D:\test\','bak',@Date,0
复制代码


五,查看磁盘驱动的空闲空间
存储过程 sys.xp_fixeddrives用于查看磁盘驱动器剩余(free)的空间
  1. exec sys.xp_fixeddrives
复制代码

1.png

六,执行DOS命令操作文件
存储过程sys.xp_cmdshell 用于执行DOS命令,该功能对应SQL Server系统的xp_cmdshell高级选项,默认情况下,该选项是禁用的,执行该存储过程,系统会抛出错误消息:

SQL Server blocked access to procedure 'sys.xp_cmdshell' of component 'xp_cmdshell' because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of 'xp_cmdshell' by using sp_configure. For more information about enabling 'xp_cmdshell', search for 'xp_cmdshell' in SQL Server Books Online.

因此,在执行该存储过程之前,必须启用xp_cmdshell选项,由于启用该选项有潜在的风险,建议用户在执行代码之后,禁用该选项。
1,启用/禁用xp_cmdshell选项
xp_cmdshell选项属于系统的高级选项,执行以下代码,允许用户修改高级选项:
  1. -- To allow advanced options to be changed.  
  2. exec sys.sp_configure 'show advanced options', 1;  
  3. go  
  4. -- To update the currently configured value for advanced options.  
  5. reconfigure;  
  6. go  
复制代码

使用以下代码启用xp_cmdshell选项:
  1. -- To enable the feature.  
  2. exec sys.sp_configure 'xp_cmdshell', 1;  
  3. go  
  4. -- To update the currently configured value for this feature.  
  5. reconfigure;  
  6. go
复制代码


使用以下代码禁用xp_cmdshell选项:
  1. -- To disable the feature.  
  2. exec sys.sp_configure 'xp_cmdshell', 0;  
  3. go  
  4. -- To update the currently configured value for this feature.  
  5. reconfigure;  
  6. go
复制代码


2,常用的DOS命令
该存储过程使得用户可以通过TSQL命令执行DOS命令,参数是命令字符串:
  1. exec sys.xp_cmdshell 'command_string'
复制代码

2.1 建立新文件或增加文件内容
格式:ECHO 文件内容>file_name
  1. exec master.dbo.xp_cmdshell 'echo abc > D:\share\test.txt'
复制代码

2.2 查看文件内容
格式:TYPE file_name
  1. exec master.dbo.xp_cmdshell 'type D:\share\test.txt'
复制代码

2.3 复制文件
格式: COPY  file_name  new_folder
  1. exec master.dbo.xp_cmdshell 'copy D:\test\test.txt D:\share\'
复制代码

2.4 显示目录
格式:DIR folder
  1. exec master.dbo.xp_cmdshell 'dir D:\share\'
复制代码

2.5 创建目录
格式:MD folder_name
  1. exec master.dbo.xp_cmdshell 'md D:\share\test\'
复制代码

2.6 删除目录
格式:RD folder
  1. exec master.dbo.xp_cmdshell 'rd D:\share\test'
复制代码

2.7 删除文件
格式:DEL file_name
  1. exec master.dbo.xp_cmdshell 'del D:\share\test.txt'
复制代码

2.8 重命名文件
格式:REN [盘符:][路径]〈旧文件名〉〈新文件名〉
  1. exec master.dbo.xp_cmdshell 'ren D:\test\test.txt new.txt'
复制代码

2.9 移动文件
格式:MOVE  file_name new_folder
  1. exec master.dbo.xp_cmdshell 'move D:\test\new.txt D:\share\'
复制代码

2.10 切换目录
格式:CD[盘符:][路径名][子目录名]
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

客服中心
关闭
在线时间:
周一~周五
8:30-17:30
QQ群:
653541906
联系电话:
010-85786021-8017
在线咨询
客服中心

意见反馈|网站地图|手机版|小黑屋|EPS数据狗论坛 ( 京ICP备09019565号-3 )   

Powered by BFIT! X3.4

© 2008-2028 BFIT Inc.

快速回复 返回顶部 返回列表