SQL Server Concatenate Error with TSQL

u1ehiz5o  于 2023-10-15  发布在  其他
关注(0)|答案(1)|浏览(115)

What is wrong with the following code?

BULK INSERT test
FROM 'myfile_'+ CONVERT(VARCHAR(20), GETDATE(), 112) + '.TXT'
    WITH
    (FIRSTROW = 2,
     FIELDTERMINATOR = '~',
     ROWTERMINATOR = '\n')

Thanks

vc6uscn9

vc6uscn91#

You can't dynamically concatenate the date to the filename in the bulk insert statement...

If you want to do this, you'll have to build up the statement using dynamic Sql then execute it:

DECLARE @Sql NVARCHAR(MAX)
SET @Sql = 
'BULK INSERT test
FROM ''myfile_' + CONVERT(VARCHAR(20), GETDATE(), 112) + '.TXT''
    WITH
    (FIRSTROW = 2,
     FIELDTERMINATOR = ''~'',
     ROWTERMINATOR = ''\n'')'

EXEC(@Sql)

相关问题