0

I have files that I need to bulk insert into SQL server:

@Date char(8) = '20200429' *--this is a specific file variable*

so the paths are as follows:

root path: @ROOT = 'G:\app\Data\table-dump\'
filepath: @FILEPATH = @date+'\'+@Date+'-230000-file.txt'
fullpath: @FULLPATH = @ROOT + @FILEPATH

I want to run a bulk insert into a table called dbo.TableDump_IMPORT

SO I constructed this:

DECLARE @BULKINSERT as varchar(max) 
SET @BULKINSERT= 'BULK INSERT dbo.TableDump_IMPORT FROM ' + @FULLPATH + ' WITH (    FIRSTROW = 2,   FIELDTERMINATOR = ''|'',    ROWTERMINATOR = ''0x0A'')'
EXEC @BULKINSERT

When I run the command I get this error:

The name 'BULK INSERT dbo.TableDump_IMPORT FROM G:\app\Data\table-dump\20200429\20200429-230000-file.txt WITH ( FIRSTROW = 2,   FIELDTERMINATOR = '|',  ROWTERMINATOR = '0x0A')' is not a valid identifier.

How do I get it to just execute the BULK INSERT command?

1
  • 1
    EXEC (@BULKINSERT) Commented May 7, 2020 at 12:27

1 Answer 1

1

You need to enclose the filepath in single quotes.

SET @BULKINSERT= 'BULK INSERT dbo.TableDump_IMPORT FROM ''' + @FULLPATH + ''' WITH ( FIRSTROW = 2, FIELDTERMINATOR = ''|'', ROWTERMINATOR = ''0x0A'')' 
Sign up to request clarification or add additional context in comments.

1 Comment

A combination of this and EXEC (@BULKINSERT) worked

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.