-->

How can I a SQL Server Agent job if a file exists

2020-05-02 07:46发布

问题:

I need to import a flat file daily. The file changes its name every day. After the file is processed, it needs to be moved to another folder.

I noticed I can schedule jobs in the SQL Server Agent, and that I can tell it to run every hour or so and that I am able to add CMD commands to it.

The solution I found was to run a script to check if the file exists, since the folder should be empty or have at least one file.

If the file exists, the script renames the file to one used in the SSIS package and then it runs the SSIS package.

After the whole thing is done, it should rename the file again based on today's date and move it to another folder.

If the file does not exist, then it should do nothing and wait another hour or so to run again.

What's the best solution to this scenario? Is the script a good idea? Maybe is it possible to add the if/else -for the file exists- into the SSIS package? Or even make the script run from the SSIS package itself instead of adding it to the Server Agent?

EDIT:

It seems I was a little naïve, it's possible to run VB scripts from the server. Would that be the recommended solution? It does solve my problem, but I'm just wondering if it's a good idea.

回答1:

This solves all my questions:

http://www.sqlservercentral.com/articles/Integration+Services+%28SSIS%29/90571/