Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2007-11-13
5
Medium Priority
?
730 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 1600 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 400 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

609 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