Monday, April 3, 2017

SQL and EXCEL - How to get Sheet names from an Excel Workbook using SQL, then use those to cycle through each sheet. Import data from multiple excel sheets.


CREATE TABLE [dbo].[STFNAMES](
[TABLE_CAT] [nchar](10) NULL,
[TABLE_SCHEM] [nchar](10) NULL,
[TABLE_NAME] [varchar](50) NULL,
[Table_Type] [varchar](50) NULL,
[Remarks] [varchar](50) NULL
) ON [PRIMARY]

GO


exec sp_dropserver 'ExcelSource','droplogins'
exec sp_addlinkedserver 'ExcelSource','', 'Microsoft.ACE.OLEDB.12.0','E:\myfile.xlsx',NULL, 'Excel 12.0'
exec sp_addlinkedsrvlogin 'ExcelSource','false'
delete STFNAMES
insert into STFNAMES EXEC sp_tables_ex 'ExcelSource'
drop table STFNAMES2
select ROW_NUMBER() OVER(ORDER BY TableNm ASC) AS Rownum, TableNm into STFNAMES2 from (
select distinct left(right(table_name,len(Table_Name)-1),charindex('$',Table_Name)-1) as TableNm from stfnames where Table_Name not like '%Name%' and Table_Name not like '%Cases%') as Tbl
select * from STFNAMES2


1 comment: