Solved

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

Posted on 2007-11-13
5
685 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how the fundamental information of how to create a table.

760 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

20 Experts available now in Live!

Get 1:1 Help Now