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

使用SQL Server判断文件是否存在后再删除(详解)

程序员文章站 2023-11-16 21:49:52
在sql server中可以使用系统内部存储过程xp_fileexist判断文件是否存在,如果存在再使用xp_cmdshell删除文件。xp_fileexist除了可以判断...

在sql server中可以使用系统内部存储过程xp_fileexist判断文件是否存在,如果存在再使用xp_cmdshell删除文件。xp_fileexist除了可以判断文件是否存在外,还可以判断文件夹是否存在,下面是下使用这两个的示例。

删除文件存储过程
alter proc [dbo].[delfile_p]
( @path nvarchar(200))
as
declare @result int
exec master.dbo.xp_fileexist @path,@result out --路径可以有空格
if @result = 1 --1存在该文件,0不存在
begin
    --如果路径有空格,在执行cmdshell前必须替换空格字符,用双引号括住
    set @path = 'del ' + replace(@path,' ','" "')
    exec master.dbo.xp_cmdshell @path
end
调用存储过程 exec mis.dbo.delfile_p 'f:/internet explorer 6 绿色版/install.log'