Sunday, March 11, 2012

BULK INSERT Help

I could use some help with BULK INSERT. It works fine unless I try to pass a variable. Check out the code below.

This works fine:

Code Snippet

BULK INSERT EPCJ_Input

FROM 'c:\Census_20070901.csv'

WITH

(

FIRSTROW = 2,

FIELDTERMINATOR = ','

)

But this does not:

Code Snippet

DECLARE @.filename VARCHAR(255)

SET @.filename = 'c:\Census_20070901.csv'

BULK INSERT EPCJ_Input

FROM @.filename

WITH

(

FIRSTROW = 2,

FIELDTERMINATOR = ','

)

This is the error I get:

Msg 102, Level 15, State 1, Line 13

Incorrect syntax near '@.filename'.

Msg 319, Level 15, State 1, Line 14

Incorrect syntax near the keyword 'with'. If this statement is a common table expression or an xmlnamespaces clause, the previous statement must be terminated with a semicolon.

Any ideas? I've tried all kinds of quote options around the @.filename.

Thanks,

Chuck

Give this a shot

Code Snippet

DECLARE @.bulk_cmd NVARCHAR(1000),

@.filename VARCHAR(255)

SET @.filename = 'c:\Census_20070901.csv'

SET @.bulk_cmd = 'BULK INSERT EPCJ_Input FROM ''' + @.filename + ''' WITH ( FIRSTROW = 2, FIELDTERMINATOR = '','' )'

EXEC sp_executesql @.statement=@.bulk_cmd

|||Beautiful, thank you.

No comments:

Post a Comment