欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SQL Server 文件操作方法

程序员文章站 2023-10-31 12:48:40
在master数据库中,sql server提供系统扩展的存储过程,其中有一些存储过程的命名以xp_开头,用于处理操作系统的文件。 一,判断文件是否存在 存储过程sys...

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

一,判断文件是否存在

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

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

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

SQL Server 文件操作方法

二,创建子目录

存储过程 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'

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

SQL Server 文件操作方法

四,删除文件

存储过程 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

SQL Server 文件操作方法

六,执行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 文件操作方法,希望对大家有所帮助