BULK INSERT COMMAND: Using both a variable and a '\n' row terminator

  • I want to write a bulk insert statement that utilizes a variable:

    Bulk Insert #TABLE

    FROM @SOURCE

    WITH (ROWTERMINATOR = '')

    I know the common way to do this is to use the EXEC command, which I wrote as:

    SET @RUNNER =

    'Bulk Insert #TABLE

    FROM' + @SOURCE +

    'WITH (ROWTERMINATOR = '''')'

    EXEC(@RUNNER)

    However, I'm getting a consistent error around the rowterminator. The EXEC command doesn't seem to like it, and my attempts to reformat it have failed.

    Is there a way to use a variable and this row terminator?

  • Don't forget to add the necessary blanks around FROM and WITH:

    ET @RUNNER =

    'Bulk Insert #Table

    FROM ' + @SOURCE +

    ' WITH (ROWTERMINATOR = '''')'

    For the row terminator - see BOL--->Specifying Field and Row Terminators.

  • This shouldn't give any erros. you can use this

    BULK INSERT <Table Name>

    FROM <File Location> (Full path)

    WITH

    (

    FIELDTERMINATOR ='|',

    ROWTERMINATOR = ''

    )

  • dmoldovan (12/4/2009)


    Don't forget to add the necessary blanks around FROM and WITH:

    ET @RUNNER =

    'Bulk Insert #Table

    FROM ' + @SOURCE +

    ' WITH (ROWTERMINATOR = '''')'

    For the row terminator - see BOL--->Specifying Field and Row Terminators.

    Still getting errors on this one.

    One thing I am doing is using a UNC path for my filepath. Aka \\server\xxxxx

    Bulk Insert has never had a problem with it, is there an issue when used in an EXEC statement?

  • Can you post the error message?

  • dmoldovan (12/4/2009)


    Can you post the error message?

    MSG 102, LEVEL 15, STATE 1, Line 1

    Incorrect syntax near '\'.

    I should mention I have also tried escape all the slashes with [] and '' but neither seemed to work.

  • I FIGURED IT OUT!

    The problem wasn't the slashes....the problem was a bulk insert statement requires its path put in quotes.

    when I did this (set @var = '\\stuff')....I was providing the command \\stuff......not '\\stuff'.

    Once I changed it to set @var = '''\\stuff''' it worked like a charm.

    Thanks for everyone's help, this one was maddening.

  • Try something like

    SET @source = '''\\server\xxxxx'''

  • i got your problem....The server should be FTP and Folder should be shared...

    If you get a chance place the error message..it would helpful to debug...

Viewing 9 posts - 1 through 8 (of 8 total)

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