tpatten
asked on
SQL2K: BULK INSERT: Check if files exists
I need to check to see if a file exists before I attempt a BULK INSERT in SQL Server 2K. I have tried code *similar* to the following:
INSERT TBL_Export_Tracking FROM 'C:\last.track' WITH (FIELDTERMINATOR = ',' ,ROWTERMINATOR = '\n')
IF @@ERROR <> 0
BEGIN
PRINT 'boo'
END
However, the error routine is not reached SQL Server instead returns and says the file is not found. SS7 had a procedure that could check for the existence of a file, but for soem reason 2000 does not. Any ideas?
INSERT TBL_Export_Tracking FROM 'C:\last.track' WITH (FIELDTERMINATOR = ',' ,ROWTERMINATOR = '\n')
IF @@ERROR <> 0
BEGIN
PRINT 'boo'
END
However, the error routine is not reached SQL Server instead returns and says the file is not found. SS7 had a procedure that could check for the existence of a file, but for soem reason 2000 does not. Any ideas?
Add a ActiveX Script to a DTS or in your SQL Server Job like this:
Function Main()
Set fso = CreateObject("Scripting.Fi leSystemOb ject")
i = fso.FileExists("\\SERVER\S HARE\FILEN AME.TXT")
If i = False Then
Main = DTSTaskExecResult_Failed
Exit Function
End If
Main = DTSTaskExecResult_Success
End Function
Function Main()
Set fso = CreateObject("Scripting.Fi
i = fso.FileExists("\\SERVER\S
If i = False Then
Main = DTSTaskExecResult_Failed
Exit Function
End If
Main = DTSTaskExecResult_Success
End Function
ASKER
In both cases we are using the file scripting object. Off the top of your head do you know if this will conflict with Norton Utilities' Script Blocking as can happen in ASP?
ASKER
nigel -- this blows up: set objFSys = nothing even if I make it this: set @objFSys = nothing
On the syntax chekc it says that "nothing" is not a column
On the syntax chekc it says that "nothing" is not a column
try this code:
-------------------------- ---------- ---------- ------
create table #fileexists (
doesexist smallint,
fileindir smallint,
direxist smallint)
Insert into #fileexists exec master..xp_fileexist 'C:\last.track'
If exists (select doesexist from #fileexists F
where F.doesexist = 1)
Begin
INSERT TBL_Export_Tracking FROM 'C:\last.track' WITH (FIELDTERMINATOR = ',' ,ROWTERMINATOR = '\n')
Print 'File Does Exists and Running Package'
End
Else Begin
Print 'File Does Not Exists'
End
DROP TABLE #fileexists
--------------------------
create table #fileexists (
doesexist smallint,
fileindir smallint,
direxist smallint)
Insert into #fileexists exec master..xp_fileexist 'C:\last.track'
If exists (select doesexist from #fileexists F
where F.doesexist = 1)
Begin
INSERT TBL_Export_Tracking FROM 'C:\last.track' WITH (FIELDTERMINATOR = ',' ,ROWTERMINATOR = '\n')
Print 'File Does Exists and Running Package'
End
Else Begin
Print 'File Does Not Exists'
End
DROP TABLE #fileexists
Sorry - some VB crept in
declare @objFSys int
declare @FileName varchar(128)
declare @i int
select @FileName = 'C:\myfile.txt'
exec sp_OACreate 'Scripting.FileSystemObjec t', @objFSys out
exec sp_OAMethod @objFSys, 'FileExists', @i out, @FileName
if @i = 1
print 'exists'
else
print 'not exists'
exec sp_OADestroy @objFSys
declare @objFSys int
declare @FileName varchar(128)
declare @i int
select @FileName = 'C:\myfile.txt'
exec sp_OACreate 'Scripting.FileSystemObjec
exec sp_OAMethod @objFSys, 'FileExists', @i out, @FileName
if @i = 1
print 'exists'
else
print 'not exists'
exec sp_OADestroy @objFSys
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Nigel and Eugene, I need to split the points somehow. I used both of your answers in one.
I think the key for me is the stored procedure 'xp_fileexist'. I like not having to directly use the FileSystemObject and instead using SQL procedures.
I think the key for me is the stored procedure 'xp_fileexist'. I like not having to directly use the FileSystemObject and instead using SQL procedures.
xp_fileexists is an undocumented (hence unsupported) procedure. Saying that it's widely used so is probably fairly safe.
You could also use xp_cmdshell with a dir command into a temp table if you don't want to use FileSystemObject (quite a reasonable opinion).
Don't worry about the points it's not important.
You could also use xp_cmdshell with a dir command into a temp table if you don't want to use FileSystemObject (quite a reasonable opinion).
Don't worry about the points it's not important.
declare @FileName varchar(128)
declare @i int
select @FileName = 'C:\myfile.txt'
exec sp_OACreate 'Scripting.FileSystemObjec
exec sp_OAMethod @objFSys, 'FileExists', @i out, @FileName
if @i = 1
print 'exists'
else
print 'not exists'
set objFSys = nothing