Solved

Transact SQL Create table for Excel Sheet names

Posted on 2011-02-18
3
1,293 Views
Last Modified: 2012-05-11
I've got about 100 Excel files, each with multiple sheets named differently by the client.  Fortunately, the structure of each sheet is identical--only the names appear to be different.  My task is to convert the data into one big csv file for import into another product.  I'm trying to avoid opening every single Excel file to get the table names.  

I've created a loop that adds the linked servers and I'd like it also to poll the data and drop it into three tables, aaTblFederal, aaTblState and aaTblAMT, so that I may manipulate the data there.  Code attached.  Pay attention to the place that shows 'TABLENAMEHERE'.  I'm sure that I'll need to create another cursor for the table names -- I just don't know how to do it.  When you respond, just a command is fine--I'll add the necessary loops.

TIA

jr
-- Create table with the Excel file names in it:
CREATE TABLE #aaSheets 
	(Filename varchar(75))
--Import the file names from a text file
BULK INSERT #aaSheets FROM 'T:\Data_Team\CurrentJobs\ABCCompany\WorkInProgress\Milestone3\Filelist.txt'
	WITH (DATAFILETYPE='char', FIELDTERMINATOR=',', ROWTERMINATOR='\n')

DECLARE @cSheets CURSOR
DECLARE @iTemp Int
DECLARE @vFile varchar(150)
DECLARE @vServer varchar(75)
DECLARE @vTemp varchar(1)

-- Scroll through the filenames and acquire the data
SET @cSheets = CURSOR SCROLL DYNAMIC FOR
select * from #aaSheets

SET @iTemp =0

OPEN @cSheets

FETCH NEXT FROM @cSheets INTO @vFile
WHILE @@FETCH_STATUS =0
BEGIN
	SET @iTemp =@iTemp +1
	-- Create the linked server to Excel Spreadsheet

	SET @vServer = 'SvrExcel' + LTRIM(STR(@iTemp))
	SET @vFile ='T:\Data_Team\CurrentJobs\ABCCompany\WorkInProgress\Milestone3\' + @vFile

	-- Keep in case I want to drop the servers first
	-- EXECUTE sp_dropserver @vServer, 'droplogins'

	EXECUTE sp_addlinkedserver
	@server = @vServer,
	@srvproduct = 'ACE 12.0',
	@provider = 'Microsoft.ACE.OLEDB.12.0',
	@datasrc = @vFile,
	@provstr = 'Excel 12.0'

	-- (COMPLETED) First time, ran this to check out sheet names and be sure there weren't files with odd sheets (COMPLETED)
	-- EXECUTE sp_tables_ex @vServer

	-- First create the Federal, State and AMT tables
	BEGIN
		IF @iTemp =1
			SELECT * into aaTblFederal FROM SvrExcel1...['Adel - Federal$']	
			SELECT * into aaTblState FROM SvrExcel1...['Adel - State$']	
			SELECT * into aaTblAMT FROM SvrExcel1...['Adel -AMT$']	
	END
	
	-- Now, add all subsequent records from other sheets to the above tables
	BEGIN
		IF @iTemp >1
			INSERT INTO aaTblFederal
				SELECT *
				FROM @vServer...['TABLENAMEHERE']
			INSERT INTO aaTblState
				SELECT *
				FROM @vServer...['TABLENAMEHERE']
			INSERT INTO aaTblAMT
				SELECT *
				FROM @vServer...['TABLENAMEHERE']
	END

	-- The first one, create the tables that will hold the data for Fed, State, AMT:
	FETCH NEXT FROM @cSheets INTO @vFile
END

CLOSE @cSheets
DEALLOCATE @cSheets

Open in new window

0
Comment
Question by:jruhe
[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
  • 2
3 Comments
 
LVL 4

Author Comment

by:jruhe
ID: 34932949
I've read several posts on similar questions to this.  I believe that most agree that it can't be done.  In order to move on, I put this in a loop:
EXECUTE sp_tables_ex @vServer

And then I manually copied and pasted all of the names into a text file (yes, the SQL equivalent of hammer and chisel), which I then imported using a bulk insert and I completed this part of my task.

Excpert's Exchange, I'd appreciate it if you'd keep this question open until Thursday, Feb 24 (I'm still hoping for an answer).  Then it may be closed, but I feel that it should stay out there in your search engine so that people can quidkly see that it can't be done and won't waste their time on looking for an answer that doesn't exist.

Thanks all
0
 
LVL 12

Accepted Solution

by:
kselvia earned 500 total points
ID: 34934045
I haven't actually tested it, but it seems like this should work

-- Get list of sheet names
create table #result 
	(TABLE_CAT varchar(130) Null,
	TABLE_SCHEM varchar(130) NULL, 
	TABLE_NAME varchar(130) NULL, 
	TABLE_TYPE varchar(130) NULL,
	REMARKS varchar(540) NULL )

insert #result
	EXECUTE sp_tables_ex @vServer
	
-- I assume each sheet will have SOME identifying portion (Federal, State, AMT)
declare @cmd varchar(2000)

select @cmd = 
'INSERT INTO aaTblFederal
				SELECT *
				FROM ' + @vServer + '...[''' + TABLE_NAME + ''']'
FROM #result				
WHERE TABLE_NAME like '%Federal$'

exec ( @cmd )

select @cmd = 
'INSERT INTO aaTblState
				SELECT *
				FROM ' + @vServer + '...[''' + TABLE_NAME + ''']'
FROM #result				
WHERE TABLE_NAME like '%State$'

exec ( @cmd )


select @cmd = 
'INSERT INTO aaTblAMT
				SELECT *
				FROM ' + @vServer + '...[''' + TABLE_NAME + ''']'
FROM #result				
WHERE TABLE_NAME like '%AMT$'

exec ( @cmd )

Open in new window

0
 
LVL 4

Author Closing Comment

by:jruhe
ID: 34948685
Works like a charm :-)

Thanks
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This article describes a serious pitfall that can happen when deleting shapes using VBA.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

751 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