Hey folks,
This SQL thing is kind of new to me, so please forgive me if this is a really basic question.
Why can't I use a variable for the Bulk Insert command? Is there a way to get around this? Here's what I'm working with:
DECLARE@emarket_ind int,@csvfile varchar(MAX)CREATE TABLE #table01 (id int identity,email varchar(MAX),customer_no int)BULK INSERT #table01FROM @csvfileWITH (FIELDTERMINATOR = ',', ROWTERMINATOR = '\n')
Thanks in advance for any help you can provide!
Rey
The Public Theater
Thanks for the reply, Ryan!
I actually ended up googling around and found this work around, which seems to be working perfectly. After declaring @bulkinsert, I used this code:
SET @bulkinsert =
'
BULK INSERT #table01
FROM ''' + @csvfile + '''
WITH
(FIELDTERMINATOR = '','', ROWTERMINATOR = ''\n'')
EXEC (@bulkinsert)
Just in case anyone runs into the same issue.
From: Tessitura Technical Forum [mailto:forums-technical@tessituranetwork.com] On Behalf Of Ryan CrepsSent: Friday, July 30, 2010 11:52 AMTo: Rey PamatmatSubject: RE: [Tessitura Technical Forum] Bulk Insert witha variable
You can’t use a variable as a table reference in a BULK insert (or any SELECT for that matter). To do so, you need to use dynamic SQL, but I would be very careful that you know what you are doing if you decide to step into dynamic SQL!
To illustrate dynamic SQL is a bit too much for this simple reply, but one tip is to never use:
“EXEC @SQL”
But to use
Execute sp_execute_sql @SQL
If you do a quick search on dynamic SQL you should come up with a few decent hits among the clutter. SQLServerCentral.com is a good place to start.
J
-Ryan
From: Tessitura Technical Forum [mailto:forums-technical@tessituranetwork.com] On Behalf Of A. Rey PamatmatSent: Friday, July 30, 2010 6:42 AMTo: Ryan CrepsSubject: [Tessitura Technical Forum] Bulk Insert witha variable
This message was sent automatically to you by www.tessituranetwork.com because you subscribed to the Tessitura Technical Forum. You may reply to this message to post to the Technical forum or visit the site to search, read and post to the forums. In the interest of keeping the forum posts from becoming cluttered, we encourage you to delete previous message text from your reply before sending. Thank you!