Solved

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

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

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.​
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

808 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