1

I've been reading up on tutorials and other stackoverflow answers but I just can't seem to get this to work. I'm running the following script and I keep getting.

USE my_db
GO

DECLARE @TempTable TABLE (
insz nvarchar(max),
firstname nvarchar(max),
middlename nvarchar(max),
lastname nvarchar(max),
birthdate date,
street nvarchar(max),
streetnumber nvarchar(max),
mailbox nvarchar(max),
city nvarchar(max),
zipcode nvarchar(max)
)
GO

BULK INSERT @TempTable
FROM 'C:\Workspaces\magdasync\src\main\examples\my_file.csv'
WITH
(FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n')
GO

Msg 102, Level 15, State 1, Line 2 Incorrect syntax near '@TempTable'.

kenny
  • 1,157
  • 1
  • 16
  • 41
  • 1
    Remove `GO` before `BULK` statement. Table variable is visible only in its scope. Also it turns out that you can not insert into table variable: http://stackoverflow.com/questions/14113820/whats-wrong-with-this-sql-statement-for-table-variable-bulk-insert – Giorgi Nakeuri Sep 05 '16 at 09:03
  • Well I ended up with a different error now Msg 102, Level 15, State 1, Line 15 Incorrect syntax near '@TempTable'. Msg 319, Level 15, State 1, Line 17 Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon. – kenny Sep 05 '16 at 09:04

1 Answers1

2

It is not possible to bulk insert in table variable. so you can use temp table.

USE magdasync
    GO

CREATE Table #TempTable(
insz nvarchar(max),
firstname nvarchar(max),
middlename nvarchar(max),
lastname nvarchar(max),
birthdate date,
street nvarchar(max),
streetnumber nvarchar(max),
mailbox nvarchar(max),
city nvarchar(max),
zipcode nvarchar(max)
)
GO

BULK INSERT #TempTable
FROM 'C:\Workspaces\magdasync\src\main\examples\magdasync_input_example.csv'
WITH
(FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n')
GO
Mohit Dagar
  • 522
  • 6
  • 21
  • There are some old answers on stackoverflow that are deprecated then cause @ is certainly not working anymore – kenny Sep 05 '16 at 10:03