SQL Server 文件操做

在master數據庫中,SQL Server提供系統擴展的存儲過程,其中有一些存儲過程的命名以xp_開頭,用於處理操做系統的文件。html

一,判斷文件是否存在sql

存儲過程sys.xp_fileexist 用於判斷文件是否存在,參數是文件(file)的路徑或目錄的路徑:shell

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

該存儲過程返回的結果集有一行數據,三個字段,以下圖:數據庫

二,建立子目錄windows

存儲過程 sys.xp_create_subdir 用於建立子目錄,參數是子目錄的路徑:this

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

執行存儲過程,系統返回消息:Command(s) completed successfully,說明子目錄建立成功。spa

三,查看子目錄結構操作系統

存儲過程sys.xp_dirtree 用於顯示當前目錄的子目錄,該存儲過程有三個參數:code

  • directory:第一個參數是要查詢的目錄;
  • depth :第二個參數是要顯示的子目錄的深度,默認值是0,表示顯示全部的子目錄;
  • file :第三個參數是bool類型,指定是否顯示子目錄中的文件(file),默認值是0,表示不顯示任何文件,只顯示子目錄(directory);
exec master.sys.xp_dirtree 'D:\data'

該存儲過程返回的字段有子目錄名稱和相對深度,返回的結果中並無顯示子目錄的父子關係:component

 

四,刪除文件

存儲過程 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 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命令詳解

相關文章
相關標籤/搜索