12

I'm trying to insert a CSV into a Temporary table and this SQL statement doesn't seem to work.

DECLARE @TempTable TABLE (FName nvarchar(max),SName nvarchar(max),
 Email nvarchar(max));
BULK INSERT @TempTable 
FROM 'C:52円BB30AD694A62A03E.csv' 
WITH (FIELDTERMINATOR = ',',ROWTERMINATOR = '\n')

Getting the following error....

Incorrect syntax near '@TempTable'.

RBarryYoung
57k15 gold badges99 silver badges142 bronze badges
asked Jan 1, 2013 at 20:13
10
  • 2
    You cannot use table variable, you need real table when using BULK INSERT Commented Jan 1, 2013 at 20:19
  • 1
    @Woot4Moo which part? Can you explain? And can you post answer how to do insert into table variable, not use temp table like you did in your answer? Commented Jan 1, 2013 at 20:24
  • 1
    @Woot4Moo you said that my comment is not true. so i wanted to know which part of my comment is not true :) Commented Jan 1, 2013 at 20:38
  • 1
    @Woot4Moo its a temp table name not variable Commented Jan 1, 2013 at 20:40
  • 2
    @Woot4Moo: Just in case you aren't sure yet how #tables are different from @tables, have a look at this question: What's the difference between a temp table and table variable in SQL Server?. Commented Jan 2, 2013 at 13:10

4 Answers 4

21

You cannot BULK INSERT into a table variable. So this line:

BULK INSERT @TempTable 

Is what is causing the error.


FYI, the simplest fix for this is probably just to use a #Temp table instead of a Table Variable. So your SQL code would change to this:

CREATE TABLE #TempTable (FName nvarchar(max),SName nvarchar(max),
 Email nvarchar(max));
BULK INSERT #TempTable 
FROM 'C:52円BB30AD694A62A03E.csv' 
WITH (FIELDTERMINATOR = ',',ROWTERMINATOR = '\n')
answered Jan 1, 2013 at 20:17

Comments

6

You cannot use table variable when using BULK INSERT

You can try this

DECLARE @TempTable TABLE (FName nvarchar(max),SName nvarchar(max),
 Email nvarchar(max));
INSERT INTO @TempTable
select * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0','Text;Database=C:\Users\Administrator\Dropbox\Personal\testing.vineup.com\admin\imported;HDR=Yes;FORMAT=Delimited(,)', 'SELECT * FROM [52BB30AD694A62A03E.csv]')
answered Jan 1, 2013 at 20:27

Comments

1

you can not use bulk insert for table variable. for that you have create temp table like below.

CREATE TABLE #TEMPtbl 
(
 [FNAME] [nvarchar](MAX) ,
 [SNAME] [nvarchar](MAX) ,
 [EMAIL] [nvarchar](MAX) 
)
GO 
BULK INSERT #TEMPtbl FROM 'C:\FileName.csv' 
WITH (FIRSTROW = 1, FIELDTERMINATOR = ',', ROWTERMINATOR = '\n')

you can try This one. it may be help you.

answered Jan 2, 2013 at 4:55

Comments

-1

I think you want to do something like this:

DECLARE @sql NVARCHAR(8000)
SET @sql = 
'
BULK INSERT #TempTable ...' ;

What you are doing is trying to force a variable into a non-dynamic sql statement. So the compiler/interpreter (not sure which is the correct term for SQL) is bombing out since it cannot properly parse it.

answered Jan 1, 2013 at 20:17

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.