0

I Have created a stored procedure to import a text file in sql database with a specific path, But my problem is the file that I am going to import in keep changing regularly, So I have to change the file name in procedure to import newly added file. I want a procedure to import new file without changing file name in the mention path. Can you help me to overcome from this problem??

Thank You in advance.

  • 1
    Have a look at this: https://stackoverflow.com/questions/16076309/import-multiple-csv-files-to-sql-server-from-a-folder – Praneet Nadkar Mar 16 '18 at 06:22
  • @PraneetNadkar, that solution relies on xp_cmdShell, which is often disabled for security reasons. Still might work, but calling it out. – Eric Brandt Mar 16 '18 at 11:28

2 Answers2

0

The following portal has sample code find latest file in a directory. https://blogs.msdn.microsoft.com/oldnewthing/20120801-00/?p=6993

It could be used to extract last latest file from the filesystem and read based on the consistency of data.

Adarsh
  • 52
  • 7
0

Personally, I'd use an SSIS package. Roughly three steps.

  1. Check for any file in the pick up folder (or with a filter based on the file extension, but not restricted by file name).
  2. Import the file if it exists.
  3. Archive the file to clear the pick up folder.

Then just schedule it to run periodically, based on your use case.

Eric Brandt
  • 7,886
  • 3
  • 18
  • 35