Check whether FileExists in SQL Server 2005/2008

Well, you might have done programming in any of the .NET flavor to check whether file is exist in particular folder or not. You might have used many classis of SYSTEM.IO namespaces and big code to check but If I tell you it is more than easy to check the file whether it is exists or not, in SQL Server than .NET, what would be your reaction? Ohh!! What are you talking about!!!!
I am not kidding, it is really very very easy, just one simple statement, no namespace, no use of class etc. etc. Have a look at it.
exec master.dbo.xp_fileexist‘d:\emps.txt’



If emps.txt would be exists in your “D” drive, you would get answer “1” in “File Exist” column or “0”. Isn’t it very easy?
Now think if you are doing some kind of T-SQL Programming and you need this value in variable, again very easy and small code snippet required for that, have a look at it again.
DECLARE @isExists INT
exec master.dbo.xp_fileexist‘d:\emps.txt’, @isExists OUTPUT
SELECT case @isExists when 1 then ‘Yes’ else ‘No’ end as isExists



Happy T-SQLing!!!
Reference: Ritesh Shah
http://www.sqlhub.com
Note: Microsoft Books online is a default reference of all articles but examples and explanations prepared by Ritesh Shah, founder of
http://www.SQLHub.com
Microsoft SQL Server Blog. Fight the fear of SQL with SQLHub.com. Founder is Ritesh Shah