数据库 
首页 > 数据库 > 浏览文章

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

(编辑:jimmy 日期: 2024/10/21 浏览:3 次 )

在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'

上一篇:解析如何在sqlserver代理中配置邮件会话
下一篇:深入sql server 2005 万能分页存储过程的详解
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网 SiteMap