6

I am trying to do a BULK INSERT operation on SQL Server 2008 R2 using a parameter. However, I am having problems that the function expects a STRING where I'm passing it a VARCHAR. My SQL is below.

DECLARE @filepath VARCHAR(30)
DECLARE @current_symbol VARCHAR(30)

DECLARE symbol_cursor CURSOR FOR
SELECT symbol FROM stocks.dbo.description WHERE 1=1

OPEN symbol_cursor;

FETCH NEXT FROM symbol_cursor INTO @current_symbol

WHILE @current_symbol is not null
BEGIN
SET @filepath = 'C:\Users\stkerr\Desktop\stockPricing\' + @current_symbol + '.prices'
BULK
INSERT stocks.dbo.pricing
FROM @filepath
WITH
(
    FIRSTROW = 2,
    FIELDTERMINATOR=',',
    ROWTERMINATOR='\n',
    ORDER   ( [date], [open], high, low, [close],volume),
        ERRORFILE='C:\Users\stkerr\errors.txt'.
)

FETCH NEXT FROM symbol_cursor INTO @current_symbol
END 
GO

The problem is popping up when I'm executing the SET @filepath statement.

Any ideas?

3 Answers 3

7

In order to specify the file in a variable (@filepath) to BULK INSERT, you will need to construct dynamic TSQL and execute it.

e.g.

DECLARE @str_command nvarchar(150)
SET @str_command = 'BULK INSERT [Customer_Sample] FROM ''' + @SourceFilePath + 
                   ''' WITH (formatfile = ''' + @FormatFilePath + 
                   ''', firstrow =' + cast(@RowNumber as nvarchar) + ')'
EXEC SP_EXECUTESQL @str_command
Sign up to request clarification or add additional context in comments.

Comments

2

there is no "string" type in sql. if you're having trouble with the set statement it's likely because you've given it a maximum length of 30 characters, and your text is longer than that.

if its the From @filepath thats giving you trouble, its because bulk insert won't take a variable in the from statement.

overall, i think your option is probably to execute the bulk insert via dynamic sql. build the statement up in a variable and then exec() it.

Comments

1

I agree with Mitch Wheat, Dynamic SQL is the way to go. You also have a couple of other things that need addressing I think. Your filepath needs to be enclosed in quotes, and I think there might be a stray dot after your error file name. Try something like;

DECLARE @filepath VARCHAR(30)
DECLARE @current_symbol VARCHAR(30)
DECLARE @sql VARCHAR(8000)

SET QUOTED_IDENTIFIER OFF

SET @current_symbol = (SELECT "tst") 

IF @current_symbol is not null
BEGIN
SET @filepath = "C:\Users\stkerr\Desktop\stockPricing\" + @current_symbol + ".prices"

SET @sql = "BULK INSERT stocks.dbo.pricing FROM '" + @filepath + "' WITH ( FIRSTROW = 2, FIELDTERMINATOR=',', ROWTERMINATOR='\n', ORDER   ( [date], [open], high, low, [close],volume), ERRORFILE='C:\Users\stkerr\Desktop\stockPricing\errors.txt') "
END 

PRINT @SQL -- Check out the result of this, and try and run it by itself.

EXEC (@SQL)

Good luck. Note my example doesn't work with dynamic SQL as Mitch suggested. Get it working first, then re-implement with Mitch's example.

Comments

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.