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:\52BB30AD694A62A03E.csv'
WITH (FIELDTERMINATOR = ',',ROWTERMINATOR = '\n')
Getting the following error....
Incorrect syntax near '@TempTable'.
BULK INSERT loads data from a data file into a table. This functionality is similar to that provided by the in option of the bcp command; however, the data file is read by the SQL Server process. For a description of the BULK INSERT syntax, see BULK INSERT (Transact-SQL).
The bulk insert operation is broken into batches, each batch is treated in its own transaction so the whole operation isn't treated under a single transaction.
Why does bulk insert lock the entire table? Specifies that a table-level lock is acquired for the duration of the bulk-import operation. A table can be loaded concurrently by multiple clients if the table has no indexes and TABLOCK is specified.
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:\52BB30AD694A62A03E.csv'
WITH (FIELDTERMINATOR = ',',ROWTERMINATOR = '\n')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With