56

解决方案: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

4 回答 4

117

像这样创建一个函数:

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 的帐户具有足够的权限来访问文件夹和文件。只读应该足够了。

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

于 2012-07-31T12:45:52.680 回答
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
于 2012-07-31T12:43:11.267 回答
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
于 2012-07-31T12:58:46.640 回答
0

您可以使用游标实现此目的,但性能比 whileloop 慢得多。这是代码:

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
于 2013-04-24T04:11:02.057 回答