在master数据库中,SQL Server提供系统扩展的存储过程,其中有一些存储过程的命名以xp_开头,用于处理操作系统的文件。

一,判断文件是否存在

存储过程sys.xp_fileexist 用于判断文件是否存在,参数是文件(file)的路径或目录的路径:

exec master.sys.xp_fileexist 'D:\test.txt'

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

二,创建子目录

存储过程 sys.xp_create_subdir 用于创建子目录,参数是子目录的路径:

exec master.sys.xp_create_subdir 'D:\test'

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

三,查看子目录结构

存储过程sys.xp_dirtree 用于显示当前目录的子目录,该存储过程有三个参数:

  • directory:第一个参数是要查询的目录;
  • depth :第二个参数是要显示的子目录的深度,默认值是0,表示显示所有的子目录;
  • file :第三个参数是bool类型,指定是否显示子目录中的文件(file),默认值是0,表示不显示任何文件,只显示子目录(directory);
exec master.sys.xp_dirtree 'D:\data'

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

四,删除文件

存储过程 sys.xp_delete_file 用于删除文件,该存储过程有5个参数:

  • 第一个参数是文件类型(File Type),有效值是0和1,0是指备份文件,1是指报表文件;
  • 第二个参数是目录路径(Folder Path), 目录中的文件会被删除,目录路径必须以“\”结尾;
  • 第三个参数是文件的扩展名(File Extension),常用的扩展名是'BAK' 或'TRN';
  • 第四个参数是Date,早于该日期创建的文件将会被删除;
  • 第五个参数是子目录(Subfolder),bool类型,0是指忽略子目录,1是指将会删除子目录中的文件;

该存储过程并不可以删除所有的文件,系统限制它只能删除特定类型的文件。

declare @Date datetime = dateadd(day,-30,getdate())
exec master.sys.xp_delete_file 0,'D:\test\','bak',@Date,0

五,查看磁盘驱动的空闲空间

存储过程 sys.xp_fixeddrives用于查看磁盘驱动器剩余(free)的空间

exec sys.xp_fixeddrives

六,执行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选项属于系统的高级选项,执行以下代码,允许用户修改高级选项:

-- To allow advanced options to be changed. 
exec 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 sp_configure 'xp_cmdshell', 1; 
go 
-- To update the currently configured value for this feature. 
reconfigure; 
go

使用以下代码禁用xp_cmdshell选项:

-- To disable the feature. 
exec 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[盘符:][路径名][子目录名]

总结

以上所述是小编给大家介绍的SQL Server 文件操作方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

点赞(100)

评论列表共有 0 条评论

立即
投稿
返回
顶部