Sometimes we need to judge whether some physical files exist, and then do some processing, such as importing a txt file regularly for a job, we need to judge whether the file exists in advance, and then do the import operation, we can use xp_fileexist to judge, the new method is as follows :
CREATE FUNCTION fun_ExistFile ( @filePath VARCHAR(800) ) RETURNS INT AS BEGIN DECLARE @result INT --Return value 1 means yes 0 means no EXEC MASTER.dbo.xp_fileexist @filePath, @result OUT RETURN @result END
The test is as follows:
SELECT dbo.fun_ExistFile('c:\data.txt')
result:
Test for the absence of:
SELECT dbo.fun_ExistFile('c:\aaaaa.txt')
result:
We can use this method to determine whether the file exists and do subsequent processing.