Bulk Insert - Dynamic Errorfile Filename

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chompy
    New Member
    • Mar 2008
    • 2

    #1

    Bulk Insert - Dynamic Errorfile Filename

    I am trying to dynamically create the errorfile name with the date the error occured. The error occurs at ERRORFILE = @errorfilename

    Is this possible?

    Here is the script I have so far...

    -------------------------------------------------------------------------------------------------------------------

    USE Temp

    GO

    DECLARE @errorfilename text

    SET @errorfilename = 'c:\rx_data_rep os_ERROR_' + CONVERT(text, GETDATE())

    BULK INSERT downloaded
    FROM 'c:\rx_data_rep os.txt'
    WITH
    (
    FIELDTERMINATOR = '|',
    ROWTERMINATOR = '\n',
    CHECK_CONSTRAIN TS,
    FIRE_TRIGGERS,
    TABLOCK,
    ERRORFILE = @errorfilename)
    )
  • ck9663
    Recognized Expert Specialist
    • Jun 2007
    • 2878

    #2
    Originally posted by chompy
    I am trying to dynamically create the errorfile name with the date the error occured. The error occurs at ERRORFILE = @errorfilename

    Is this possible?

    Here is the script I have so far...

    -------------------------------------------------------------------------------------------------------------------

    USE Temp

    GO

    DECLARE @errorfilename text

    SET @errorfilename = 'c:\rx_data_rep os_ERROR_' + CONVERT(text, GETDATE())

    BULK INSERT downloaded
    FROM 'c:\rx_data_rep os.txt'
    WITH
    (
    FIELDTERMINATOR = '|',
    ROWTERMINATOR = '\n',
    CHECK_CONSTRAIN TS,
    FIRE_TRIGGERS,
    TABLOCK,
    ERRORFILE = @errorfilename)
    )

    You might need to create a dynamic t-sql for this one

    declare @sqlstmt varchar(150)

    set @sqlstmt = 'BULK INSERT downloaded
    FROM ''c:\rx_data_re pos.txt''
    WITH
    (
    FIELDTERMINATOR = ''|'',
    ROWTERMINATOR = ''\n'',
    CHECK_CONSTRAIN TS,
    FIRE_TRIGGERS,
    TABLOCK,
    ERRORFILE = ' + @errorfilename + ')'

    exec @sqlstmt

    I did not check if you BULK INSERT syntax is correct. But I hope you got the idea of what am trying to suggest.

    -- CK

    Comment

    • chompy
      New Member
      • Mar 2008
      • 2

      #3
      Thanks CK,

      This is what I ended up with.

      DECLARE @sqlstmt varchar(500),
      @errorfilename varchar(150),
      @TodayDate as varchar(40),
      @TodayHour as varchar(40),
      @TodayMinu as varchar(40)

      SELECT @TodayDate = CONVERT(varchar (10), GETDATE(), 112)
      SELECT @TodayHour = DATEPART(hh,GET DATE())
      SELECT @TodayMinu = DATEPART(mi,GET DATE())

      SET @errorfilename = 'c:\rx_data_rep os_Error_' +
      @TodayDate +
      '_' +
      @TodayHour +
      @TodayMinu +
      '.txt'

      SET @sqlstmt = 'BULK INSERT tmp_Rx_Data_Rep os
      FROM ''c:\rx_data_re pos.txt''
      WITH (FIELDTERMINATO R = ''|'',
      ROWTERMINATOR = ''\n'',
      ERRORFILE = ' + '''' +@errorfilename + '''' +')'

      EXECUTE (@sqlstmt)

      Comment

      Working...