1. 程式人生 > 資料庫 >SQL Server 檔案操作方法

SQL Server 檔案操作方法

在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 檔案操作方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回覆大家的。在此也非常感謝大家對我們網站的支援!