2

i am importing my data from excel file to Sql Database using openrowset following are my command

declare @path varchar(100)='E:\11.08.2017 .xlsx;'

SELECT * into palwal12 FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0; Database='+@path+' HDR=YES; IMEX=1', 'SELECT * FROM [sheet1$]')

i successfully insert data into table while using simple query. but when if i want to use variable path to insert data then i m having an error as incorrect syntax near '+'. i thought my syntax for using path variable is wrong. somebody could help me please.

2 Answers 2

1

You can't use variables in openrowset parameters, they have to be string literals What you can do is create a dynamic sql and then execute it. example:

Declare @sql varchar(max) = your sql
exec(@sql)
Sign up to request clarification or add additional context in comments.

1 Comment

thanx @jayvee with the help of your answer i finally run my query
0

as @jayvee suggested me i created a dynamic Sql and execute my query as following

`DECLARE @sql varchar(MAX);
DECLARE @path varchar(255);
SET @path = 'E:\11.08.2017 .xlsx;'
SET @sql = 'SELECT * INTO ravan FROM OPENROWSET(''Microsoft.ACE.OLEDB.12.0'',
  ''Excel 12.0; Database='+@path+' HDR=YES; IMEX=1'', 
  ''SELECT * FROM [sheet1$]'')';

EXEC (@sql) 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.