-- Get table (worksheet) or column (field) listings from an excel spreadsheet
--
declare @linkedServerName sysname = 'TempExcelSpreadsheet'
declare @excelFileUrl nvarchar(1000) = 'D:\text.xlsx'
-- /SET
-- ( )
if exists(select null from sys.servers where name = @linkedServerName) begin
exec sp_dropserver @server = @linkedServerName, @droplogins = 'droplogins'
end
--
-- ACE 12.0 *.xls *.xlsx, Jet , *.xls
exec sp_addlinkedserver
@server = @linkedServerName,
@srvproduct = 'ACE 12.0',
@provider = 'Microsoft.ACE.OLEDB.12.0',
@datasrc = @excelFileUrl,
@provstr = 'Excel 12.0;HDR=Yes'
--
declare @suser_sname nvarchar(256) = suser_sname()
--
exec sp_addlinkedsrvlogin
@rmtsrvname = @linkedServerName,
@useself = 'false',
@locallogin = @suser_sname,
@rmtuser = null,
@rmtpassword = null
-- sheet sheet
exec sp_tables_ex @linkedServerName
exec sp_columns_ex @linkedServerName
--
if exists(select null from sys.servers where name = @linkedServerName) begin
exec sp_dropserver @server = @linkedServerName, @droplogins = 'droplogins'
end