How to cast variables in T-SQL for bulk insert?

后端 未结 6 1550
面向向阳花
面向向阳花 2020-11-30 10:20

The following code gives an error (its part of a T-SQL stored procedure):

-- Bulk insert data from the .csv file into the staging table.
DECLARE @CSVfile nva         


        
6条回答
  •  不知归路
    2020-11-30 10:51

    Most of the time the variable i'm looking for in a file name is the date, and this one works perfectly for bulk inserting files with date, for use such as in a daily job. Change as per your need, date format, table name, file path, file name and delimiters.

        DECLARE @DT VARCHAR (10)
        DECLARE @INSERT VARCHAR (1000)
        SET @DT = (CONVERT(VARCHAR(10),GETDATE()-1,120))
        SET @INSERT = 'BULK INSERT dbo.table FROM ''C:\FOLDER\FILE'+@DT+'.txt'''+' WITH  (FIRSTROW=2, FIELDTERMINATOR=''\t'', ROWTERMINATOR=''\n'')'
        EXEC (@INSERT);
    

提交回复
热议问题