Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Transact SQL Create table for Excel Sheet names

Posted on 2011-02-18
3
Medium Priority
?
1,310 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 2000 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

Independent Software Vendors: 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

An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

885 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