This function can be used to check if a file exists.Works with SQL Server 2000, 2005, and 2008.if object_id('dbo.F_CHECK_IF_FILE_EXISTS','FN') is not null begin drop function dbo.F_CHECK_IF_FILE_EXISTS endgocreate function dbo.F_CHECK_IF_FILE_EXISTS ( @FILEPATH nvarchar(500) )returns intas/*Function F_CHECK_IF_FILE_EXISTS returns a 1if the file passed in parameter @FILEPATH exists,and returns a 0 if is does not exist.It works with UNC names if the SQL Serverservice account has access to the share.*/begindeclare @retcode intdeclare @FILE_EXISTS intexec @retcode = master.dbo.xp_fileexist @FILEPATH, @FILE_EXISTS output return @FILE_EXISTSendgoprint 'Check if file exists'select [C:\autoexec.bat] = dbo.F_CHECK_IF_FILE_EXISTS('C:\autoexec.bat')select [D:\autoexec.bat] = dbo.F_CHECK_IF_FILE_EXISTS('D:\autoexec.bat')select [C:\autoexecx.bat] = dbo.F_CHECK_IF_FILE_EXISTS('C:\autoexecx.bat')
Results:Check if file existsC:\autoexec.bat --------------- 1(1 row(s) affected)D:\autoexec.bat --------------- 0(1 row(s) affected)C:\autoexecx.bat ---------------- 0(1 row(s) affected)
CODO ERGO SUM