检查sql server中是否存在文件?

时间:2012-07-31 12:24:57

标签: sql sql-server sql-server-2008

解决方案:http://www.tech-recipes.com/rx/30527/sql-server-how-to-check-if-a-file-exists-in-a-directory/

使用stackoverflow问题发表关于此问题的帖子以帮助其他人。

id  filepath

1   C:\vishwanath\21776656.docx
2   C:\vishwanath\vish\s_srv_req_2009.txt
3   C:\Users\dalvi\DW\DW20SharedAmd64.exe
4   C:\Users\dalvi\1.txt

我在我的数据库服务器中创建了这样的表,我在文件路径列中存储了文件路径,现在我要检查使用sql是否存在我的机器中的文件,如果它存在我需要添加临时我的表中的列显示是否存在且不存在。

我写了这个代码适用于1个文件但是我不知道如何将它用于我的表。

DECLARE @isExists INT
exec master.dbo.xp_fileexist 'C:\vishwanath\21776656.docx', 
@isExists OUTPUT
SELECT case @isExists 
when 1 then 'Yes' 
else 'No' 
end as isExists

最终输出应该是这样的

id  filepath                                 Isexists

1   C:\vishwanath\21776656.docx               Yes
2   C:\vishwanath\vish\s_srv_req_2009.txt     Yes
3   C:\Users\dalvi\DW\DW20SharedAmd64.exe     Yes
4   C:\Users\dalvi\1.txt                      No

4 个答案:

答案 0 :(得分:87)

创建一个这样的函数:

CREATE FUNCTION dbo.fn_FileExists(@path varchar(512))
RETURNS BIT
AS
BEGIN
     DECLARE @result INT
     EXEC master.dbo.xp_fileexist @path, @result OUTPUT
     RETURN cast(@result as bit)
END;
GO

编辑表并添加计算列(IsExists BIT)。将表达式设置为:

dbo.fn_FileExists(filepath)

然后选择:

SELECT * FROM dbo.MyTable where IsExists = 1

更新

使用计算列外的函数:

select id, filename, dbo.fn_FileExists(filename) as IsExists
from dbo.MyTable

更新

如果函数为已知文件返回0,则可能存在权限问题。确保SQL Server的帐户具有足够的权限来访问该文件夹和文件。只读就足够了。

并且是,默认情况下,“网络服务”帐户对大多数文件夹没有足够的权限。右键单击相关文件夹并选择“属性”,然后单击“安全”选项卡。点击“修改”,然后添加“网络服务”。单击“应用”并重新测试。

答案 1 :(得分:2)

未经测试,但你可以尝试这样的事情:

Declare @count as int
Set @count=1
Declare @inputFile varchar(max)
Declare @Sample Table
(id int,filepath varchar(max) ,Isexists char(3))

while @count<(select max(id) from yourTable)
BEGIN
Set @inputFile =(Select filepath from yourTable where id=@count)
DECLARE @isExists INT
exec master.dbo.xp_fileexist @inputFile , 
@isExists OUTPUT
insert into @Sample
Select @count,@inputFile ,case @isExists 
when 1 then 'Yes' 
else 'No' 
end as isExists
set @count=@count+1
END

答案 2 :(得分:0)

请尝试以下代码来验证文件是否存在。您可以创建用户函数并在存储过程中使用它。根据需要修改:

Set NOCOUNT ON

 DECLARE @Filename NVARCHAR(50)
 DECLARE @fileFullPath NVARCHAR(100)

 SELECT @Filename = N'LogiSetup.log'
 SELECT @fileFullPath = N'C:\LogiSetup.log'

create table #dir

(output varchar(2000))

 DECLARE @cmd NVARCHAR(100)
SELECT @cmd = 'dir ' + @fileFullPath     

insert into #dir    

exec master.dbo.xp_cmdshell @cmd

--Select * from #dir

-- This is risky, as the fle path itself might contain the filename
if exists (Select * from #dir where output like '%'+ @Filename +'%')

       begin    
              Print 'File found'    
              --Add code you want to run if file exists    
       end    
else    
       begin    
              Print 'No File Found'    
              --Add code you want to run if file does not exists    
       end

drop table #dir

答案 3 :(得分:0)

你可以使用游标实现这一点,但性能比while循环要慢得多。 这是代码:

set nocount on
declare cur cursor local fast_forward for
    (select filepath from Directory)
open cur;
declare @fullpath varchar(250);
declare @isExists int;

fetch from cur into @fullpath
while @@FETCH_STATUS = 0
    begin
        exec xp_fileexist @fullpath, @isExists out
        if @isExists = 1            
            print @fullpath + char(9) + char(9) + 'file exists'
        else            
            print @fullpath + char(9) + char(9) + 'file does not exists'
        fetch from cur into @fullpath
    end
close cur
deallocate cur

或者你可以把它放在tempTable中,如果你想把它集成到你的前端..

create proc GetFileStatus as
begin
    set nocount on
    create table #tempFileStatus(FilePath varchar(300),FileStatus varchar(30))
    declare cur cursor local fast_forward for
        (select filepath from Directory)
    open cur;
    declare @fullpath varchar(250);
    declare @isExists int;

    fetch from cur into @fullpath
    while @@FETCH_STATUS = 0
        begin
            exec xp_fileexist @fullpath, @isExists out
            if @isExists = 1                
                insert into #tempFileStatus values(@fullpath,'File exist')
            else
                insert into #tempFileStatus values(@fullpath,'File does not exists')
            fetch from cur into @fullpath
        end
    close cur
    deallocate cur
    select * from #tempFileStatus
    drop table #tempFileStatus
end

然后使用:

调用它
exec GetFileStatus