Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

150
Views
INSERT INTO table using Microsoft.ACE.OLEDB.12.0 and declared Variable

I'm trying to import the data from the Excel file to Table. Without using variable everything is working fine, however, when I try to declare var in the first place the execution ended up in error.

Script:

Declare @DateUsed NVARCHAR(30), @StringDatabase NVARCHAR(30);
SET @DateUsed = '2022-06-20.xlsx';
SET @StringDatabase = 'Database=C:\PATH -'

INSERT INTO Viator.dbo.Test SELECT *  
from  OPENROWSET(
'Microsoft.ACE.OLEDB.12.0',
'Excel 12.0;'
 @StringDatabase + @DateUsed,
'SELECT * FROM [Table1$]'
);

If I'll replace the @StringDatabase and @DateUsed the query will finish successfully.

Error message:

Msg 102, Level 15, State 1, Line 10
Incorrect syntax near '@StringDatabase'.

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Per @Larnu comment

The approach should be to define dynamic SQL based on one of the already provided solutions here

SET @DateUsed = '2022-06-20.xlsx';
SET @sql = 'INSERT INTO dbo.Test SELECT *  
from  OPENROWSET(
''Microsoft.ACE.OLEDB.12.0'',
''Excel 12.0;
Database=C:\PATH - ' + @DateUsed + ''',
''SELECT * FROM [Table1$]''
);'


exec(@sql);
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!