Solved

Import multiple Excel files to SQL table variables in a stored procedure

Posted on 2007-11-13
5
695 Views
Last Modified: 2012-06-27
There was a solution posted for a SIMILAR problem, but it isn't working for my situation.
I need to import several Exel files with a stored procedure into a table variable.
This stored procedure is being called from the web
@zfile is a parameter

I declare the table variable and the variable for the source file

Declare @Zip table(Zip3 varchar(3), zGroup varchar(25));
Declare @srcFile as varchar(200)

I set the string (the file names will change very time)       
Set @srcFile = 'Excel 8.0;Database=' + @zFile + ';HDR=Yes'      
                  
And try to import to the table variable...

INSERT INTO @Zip (Zip3, zGroup)
SELECT * FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0', @srcFile)...[Sheet1$]      


Then I get this error

Msg 102, Level 15, State 1, Procedure CreateCPTReport, Line 33
Incorrect syntax near '@srcFile'.

Is there any way you can change the file name each time, this doesn't seem to be working
and I have to do this with 4 tables every SP run
            
0
Comment
Question by:Kmarcum
5 Comments
 
LVL 23

Expert Comment

by:Ashish Patel
ID: 20277967
Why dont you create the insert string as one line like this
declare @SQL varchar(8000)
set @SQL = 'INSERT INTO ' + @Zip + ' (Zip3, zGroup) SELECT * FROM OpenDataSource( ''Microsoft.Jet.OLEDB.4.0'', ' + @srcFile + ')...[Sheet1$] '
Exec (@SQL)

Try this.
0
 
LVL 12

Accepted Solution

by:
kselvia earned 400 total points
ID: 20277970
You need dynamic SQL and that can't insert into a table variable.

declare @cmd varchar(1000)
Declare @Zip table(Zip3 varchar(3), zGroup varchar(25));
Declare @srcFile as varchar(200)

-- create temp table
create table #zip (Zip3 varchar(3), zGroup varchar(25))

Set @srcFile = 'Excel 8.0;Database=' + @zFile + ';HDR=Yes'      

set @cmd =
' INSERT INTO #Zip (Zip3, zGroup) SELECT * FROM OpenDataSource( ''Microsoft.Jet.OLEDB.4.0'', ' + @srcFile+ ')...[Sheet1$]'

exec (@cmd)

-- If you really need the table variable
insert @zip
   select * from #zip      

0
 
LVL 6

Assisted Solution

by:Rajesh_mj
Rajesh_mj earned 100 total points
ID: 20277978
0
 

Author Comment

by:Kmarcum
ID: 20314817
Sorry, I got sidetracked on another project I'll try these out over the holiday
0
 

Author Comment

by:Kmarcum
ID: 20612744
Some changes were needed on the server. As soon as it's tested I'll accept an answer.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
I have a large data set and a SSIS package. How can I load this file in multi threading?
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now