Dynamically import a file

  • Hi everyone,

    Needing some help on a challenge I'm facing. I'm not how to go about doing this.

    I want to import a file based on name and date. For example I have a directory with the following files:

    C:\TestFiles

    In this directory I have the following files:

    PRIME{02-18-2010 WEDNESDAY Group 1}.txt

    PRIME{03-04-2010 WEDNESDAY Group 1}.txt

    PRIME{03-09-2010 EIMDAILY Group 1}.txt

    PRIME{03-10-2010 EIMDAILY Group 1}.txt

    For today (3/10/10), I only want to import "PRIME{03-10-2010 EIMDAILY Group 1}.txt" with the greatest / most recent date.

    So tomorrow (3/11/10), I'll only want to import "PRIME{03-11-2010 EIMDAILY Group 1}.txt".

    How would I go about doing this? In a script? or SSIS package? I want to get this automated.

    Please help.

  • One option is to use the BULK INSERT statement.

    Example:

    DECLARE @FilePath NVARCHAR(126),

    @sql NVARCHAR(MAX);

    -- Construct the file name for today

    SET @FilePath =

    N'C:\TestFiles\PRIME{' +

    CONVERT(CHAR(10), CURRENT_TIMESTAMP, 110) +

    N' EIMDAILY Group 1}.txt';

    -- Construct the BULK INSERT statement

    SET @sql = N'BULK INSERT dbo.ImportTable FROM ' + QUOTENAME(@FilePath, NCHAR(39))

    -- Show the statement

    PRINT @sql

    -- Execute it (uncomment first!)

    --EXECUTE (@SQL);

    Be sure to check the link to the BULK INSERT documentation shown above - for syntax and additional options.

    Paul

  • Awesome! thank you very much.

  • No worries. 🙂

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply