Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends
In a situation that i want to load table in sql server from dynamic excel file.In c# code i just get the file name and i pass it to sql server as a parameter to storedprocedure.but in my stored procedure i need to import data from this particular file to the database table. upto passing the file name i can do it. but i dont know how to make query to import this excel file.
This is my stored procedure. the error is near + syntax
SQL
create procedure sp_excelforgeneralholidays(@filname nvarchar(max))
 as
 set nocount on
 --declare @sql nvarchar(max)
 begin
 --insert into General_holydays_details
 select *  from OPENROWSET('Microsoft.jet.OLEDB.4.0','Excel 8.0;DATABASE='+@filname+ 'Select Date,Day,Reason from [sheet1$])'

 end
Posted
Updated 22-Aug-12 9:37am
v3
Comments
[no name] 22-Aug-12 15:36pm    
Since you did not say what the error is try: select * into General_holydays_details from OPENROWSET('Microsoft.jet.OLEDB.4.0','Excel 8.0;DATABASE='+@filname+ ', Select Date,Day,Reason from [sheet1$])'
baskaran chellasamy 22-Aug-12 15:42pm    
the error is syntax error near +.

1 solution

You didn't post the exact error you're getting but bsaed on your query, I'd guess that you don't have apostrophes in your filename.

Also if I recall correctly the openrowset command should be concatenated to a string and then executed if you use variables.

So, have a try with something like:

SQL
...
declare sqltext varxhar(1000);
...
set sqltext = 'select * into General_holydays_details from OPENROWSET(''Microsoft.jet.OLEDB.4.0'',''Excel 8.0;DATABASE='''+@filname+ '''Select Date,Day,Reason from [sheet1$])';
print (sqltext)
execute (sqltext)
...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900