Hi @SILS ,
Maybe you can build the statement as a string first, and then use EXECUTE to run it.
You can refer to the following example:
declare @query varchar(5000)
declare @FileName varchar(100)
declare @FilePath varchar(100)
set @FilePath = 'C:\textfiles';
set @FileName = 'test.txt';
set @query = 'insert into [dbo].[ZZ_Flat_Data]
(CoCode, CoName, CoAddress1, CoAddress2, CoAddress, CoPost, SPeron, Transporter, DocDate, DocNo,
Category, Amount, Type)
SELECT * FROM OPENROWSET (''Microsoft.ACE.OLEDB.12.0'',''Text;Database=' + @FilePath + ''',
''SELECT CoCode, CoName, CoAddress1, CoAddress2, CoAddress, CoPost, SPeron, Transporter, DocDate, DocNo,
Category, Amount, Type FROM ' + @FileName +''')';
print @query
execute(@query)
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Lan Huang