Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

148
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda