Solved

Transact SQL Create table for Excel Sheet names

Posted on 2011-02-18
3
1,278 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
  • 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

759 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

21 Experts available now in Live!

Get 1:1 Help Now