Whats wrong with this SQL statement for table variable bulk insert

后端 未结 4 829
挽巷
挽巷 2020-12-10 14:25

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         


        
相关标签:
4条回答
  • 2020-12-10 14:47

    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]')
    
    0 讨论(0)
  • 2020-12-10 14:48

    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.

    0 讨论(0)
  • 2020-12-10 14:51

    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.

    0 讨论(0)
  • 2020-12-10 14:57

    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')
    
    0 讨论(0)
提交回复
热议问题