복붙노트

[SQL] 파일에 대한 확인은 SQL 서버에 존재 여부?

SQL

파일에 대한 확인은 SQL 서버에 존재 여부?

해결 방법 : http://www.tech-recipes.com/rx/30527/sql-server-how-to-check-if-a-file-exists-in-a-directory/

도움말 다른 사람에게 유래 질문을 사용하여이 문제에 대한 게시물을했다.

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

이 열을 파일 경로에 내 DB 서버에서 생성이 같은 한 표, 내가 파일 경로를 저장 한 지금 나는 내가 임시 열을 추가 할 필요가있는 경우 파일이, 내 컴퓨터에 있는지 SQL을 사용하여 확인했습니다 내 테이블에 존재하지 않고이 존재하지 않는 경우 예를 보여주는.

나는 한 파일에 대해 작동이 코드를 작성하지만 내 테이블을 사용하는 방법을 모르겠어요.

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

해결법

  1. ==============================

    1.그래서 같은 함수를 만듭니다 :

    그래서 같은 함수를 만듭니다 :

    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
    

    최신 정보

  2. ==============================

    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
    
  3. ==============================

    3.당신은 커서를 사용하여이를 달성 할 수 있지만 성능은 while 루프보다 훨씬 느립니다 .. 여기 코드는 :

    당신은 커서를 사용하여이를 달성 할 수 있지만 성능은 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
    

    당신이 당신의 프론트 엔드에 통합하려는 경우 또는 당신은 임시 테이블에 넣을 수 있습니다 ..

    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
    
  4. ==============================

    4.파일의 존재 여부를 확인하려면 다음 코드를 사용해보십시오. 당신은 사용자 함수를 작성하고이를 저장 프로 시저에 사용할 수 있습니다. 당신이 필요로 수정

    파일의 존재 여부를 확인하려면 다음 코드를 사용해보십시오. 당신은 사용자 함수를 작성하고이를 저장 프로 시저에 사용할 수 있습니다. 당신이 필요로 수정

    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
    
  5. from https://stackoverflow.com/questions/11740000/check-for-file-exists-or-not-in-sql-server by cc-by-sa and MIT license