views:

42

answers:

3

Hello all,

I'm currently tasked with reading some data that stored in a flat file into my database and run reports against it. The one problem I'm running into is checking to see if a file actually exists. Is there a simple function to check if the file exists?

Thanks!

+3  A: 

Just googling I found this at SQL DBA and this at MS SQL tips.

Kyra
Hum... seems my google skills aren't working well today. Thank you for the help!
Shaded
no problem. Glad to help :D
Kyra
A: 

I believe you can do something like this:

DECLARE @Path varchar(128) ,
 @FileName varchar(128)
 SET @Path = 'C:\'
 SET @FileName = 'FILE_NAME.EXT'

DECLARE @objFSys int
DECLARE @i int
DECLARE @File varchar(1000)

 SET @File = @Path + @FileName
 EXEC sp_OACreate 'Scripting.FileSystemObject', @objFSys out
 EXEC sp_OAMethod @objFSys, 'FileExists', @i out, @File
 IF @i = 1
  PRINT 'file exists'
 ELSE
  PRINT 'file does not exists'
 EXEC sp_OADestroy @objFSys 

This article goes over this method and a couple others.

Abe Miessler
+1  A: 

You are doing ETL in a stored procedure?!! I don't think you should, just because you can.

I recommend you use use SSIS for this. Doing ETL in Stored Proc or TSQL is not a recommended practice, in fact, it is frequently used as an example of what not to do.

Raj More
If I could +2 this, I would
ninesided
I am not, mainly because there is already a procedure that I can use to import files coded by someone else. I'm just checking to see if a file exists before sending it over to that procedure. However you've piqued my interest. Do you know of any good sources that I could learn to use SSIS from?
Shaded
@Shaded I would start with SQL Server Integration Services Step By Step book from the Microsoft press
Raj More
Raj More