EXEC a text string?

  • I'd like to replace:

    'C:\MyData\File1.csv'

    with

    'C:\MyData\ ' + @FirmCode

    Think I have to do it with an EXEC then the rest of it as a text string?? I can't seem to make it work. Any help would be appreciated. Thanks!

    DECLARE @FirmCode as varchar(5)

    SET @FirmCode = 'File1'

    BULK INSERT

    PreProcessRaw

    FROM

    'C:\MyData\File1.csv'

    WITH

    (FIELDTERMINATOR = '","',

    ROWTERMINATOR = '')

  • You have to build the entire string in a variable FIRST, then issue the exec on the variable.

    Something like:

    DECLARE @FirmCode as varchar(5)

    SET @FirmCode = 'File1'

    declare @SQL as varchar(500)

    set @SQL = 'BULK INSERT PreProcessRaw

    FROM ''C:\MyData\'+@FirmCode+'.csv''

    WITH (FIELDTERMINATOR = ''","'',

    ROWTERMINATOR = '''') '

    print @sql

    EXEC(@SQL)

    ----------------------------------------------------------------------------------
    Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?

  • Thanks that works!

    I didn't copy my bulk insert code over correct. My Row Terminator needed to look like this:

    WITH (FIELDTERMINATOR = ''","'', ROWTERMINATOR = '''')'

    All is fine now!!

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

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