Solved

SQL 2005: SSIS QUERY

Posted on 2011-09-06
9
319 Views
Last Modified: 2013-11-10
HI ALL

i am currently developing my first package in ssis, one element of the package is a data flow task that uses an ole db source - which points towards an excel file - thinking being that it will pick up this file when its saved in the correct location process it accordingly and move the file when fisnished

this is all working perfectly in debug, until when the excel document isnt there - it then gives back an error!

how would i best achieve this process? eg i will want to check the folder daily and if its not there simply do nothing and return no errors..

cheers

mal
0
Comment
Question by:malraff
  • 5
  • 3
9 Comments
 
LVL 18

Expert Comment

by:lludden
ID: 36489087
Here is what  I use as the first step in my SSIS jobs that import files.  It checks to see if the file is there and hasn't already been processed.  As the final step, I update the table ImportLog with the finished time.


DECLARE @ImportName varchar(50)
SET NOCOUNT ON
SET @ImportName = 'DailyTotals'
DECLARE @FileInfo varchar(500)
DECLARE @CreateDate datetime
CREATE TABLE [dbo].[#tbl_temp_Timestamp](vchFileData varchar(2000))
INSERT INTO #tbl_temp_Timestamp
  EXEC [master].[dbo].xp_cmdshell 'dir \\server\share\filename.txt /T:W | FIND "filename"'

SELECT @FileInfo = vchFileData FROM #tbl_temp_Timestamp WHERE #tbl_temp_Timestamp.vchFileData IS NOT NULL
DROP TABLE [dbo].[#tbl_temp_Timestamp]
IF @FileInfo = 'File Not Found'
    BEGIN
      raiserror ('File not present',16,1)
      return
    END
SELECT @CreateDate = CAST(LEFT(@FileInfo,20) AS SMALLDATETIME)

IF (SELECT COUNT(*) FROM ImportLog WHERE ImportName = @ImportName AND FileDate = @CreateDate AND ImportComplete IS NOT NULL) > 0
    BEGIN
      raiserror ('File already processed',16,1)
      return
    END

INSERT INTO ImportLog (ImportName, FileDate, ImportStart) VALUES (@ImportName, @CreateDate, getdate())

0
 
LVL 14

Accepted Solution

by:
Christopher Gordon earned 500 total points
ID: 36489143
In the past, I've used a Foreach Loop Container to prevent the "File Not Found" type of errors.

If you haven't used a Foreach Loop Container before, simply drag the Task from the toolbar onto your SSIS Canvas.  You can then copy and paste the contents of your package into the Foreach Loop Container (make sure you have the task highlighted when you paste).

Last, you'll need to set some properties of the Foreach loop task.

1. Right Click on Foreach Loop task.
2. Select "Collection" on Left Hand Menu
3. Change Enumerator to "Foreach File Enumerator".
4. Set the "Folder:" property the target folder.
5. Set the "Files:" property to the name of the file the package will be looking for (you can use wildcards).

0
 
LVL 14

Expert Comment

by:Christopher Gordon
ID: 36489181
Some more additional explanation might be in order,

With the above logic, the ssis package will first execute the Foreach loop task.  If it finds a file that matches the criteria specified, it will execute the code inside the container.  If it doesn't, it will close without throwing an error.
0
 

Author Comment

by:malraff
ID: 36489225
hi gohord

i tried the foreach loop container - but it still returns the exact same error when it does not exist? it works when the file is there
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:malraff
ID: 36489244
0
 
LVL 14

Expert Comment

by:Christopher Gordon
ID: 36489263
hi malraff

Sorry never had that issue before.

Do you have all of your code inside the foreach loop container?

Are there any other files in the target folder?  If so, is the configuration information within the task specific enough to the files you're looking to process?

0
 
LVL 14

Assisted Solution

by:Christopher Gordon
Christopher Gordon earned 500 total points
ID: 36489270
This probably isn't the issue, but might be a good idea to set "DelayValidation" property of your Data Flow task to "True".  (Since the file isn't always going to be there)
0
 

Author Comment

by:malraff
ID: 36489646
brilliant gohord! i changed delay validation to true and viola - no errors!

this isnt sitting in a loop if the files are not there?


mal
0
 
LVL 14

Expert Comment

by:Christopher Gordon
ID: 36489733
You won't have loop issues if there are no files in the specified folder in the foreach file task.  In fact, the foreach file loop won't allow the code to "kick off" if there are no files that match the criteria specified.

Glad I could help!
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now