Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i want to write a command prompt statement which will fetch me the worksheet name of any excel file on a given location, so that i can use it in my sql procedure XP_cmd_shell command.

i dont want to use linked server to fetch excel worksheet name automatically, so i want to write a DOS Command that will fetch the worksheet name via command prompt. and then I can use that Command in my Store procedure without creating any linked server.

What I have tried:

-- Get table (worksheet) or column (field) listings from an excel spreadsheet

-- SET THESE!
declare @linkedServerName sysname = 'TempExcelSpreadsheet'
declare @excelFileUrl nvarchar(1000) = 'c:\MySpreadsheet.xls'
-- /SET

-- Remove existing linked server (if necessary)
if exists(select null from sys.servers where name = @linkedServerName) begin
    exec sp_dropserver @server = @linkedServerName, @droplogins = 'droplogins'
end

-- Add the linked server
-- ACE 12.0 seems to work for both xsl and xslx, though some might prefer the older JET provider
exec sp_addlinkedserver
    @server = @linkedServerName,
    @srvproduct = 'ACE 12.0',
    @provider = 'Microsoft.ACE.OLEDB.12.0',
    @datasrc = @excelFileUrl,
    @provstr = 'Excel 12.0;HDR=Yes'

-- Grab the current user to use as a remote login
declare @suser_sname nvarchar(256) = suser_sname()

-- Add the current user as a login
exec sp_addlinkedsrvlogin
    @rmtsrvname = @linkedServerName,
    @useself = 'false',
    @locallogin = @suser_sname,
    @rmtuser = null,
    @rmtpassword = null

-- Return the table/column info
exec sp_tables_ex @linkedServerName
exec sp_columns_ex @linkedServerName

-- Remove temp linked server
if exists(select null from sys.servers where name = @linkedServerName) begin
    exec sp_dropserver @server = @linkedServerName, @droplogins = 'droplogins'
end
Posted
Updated 19-Feb-18 18:03pm
Comments
Mehdi Gholam 20-Feb-18 0:20am    
I would not recommend a linked server to an Excel file.
What are you trying to do?
CHill60 20-Feb-18 7:03am    
Now I understand why the OP is trying to use a Linked Server - it's the go-to solution for unknown name(s) of the worksheet(s). Yuk.
CHill60 20-Feb-18 3:47am    
For clarity - you have already been told that you cannot use a "DOS command" to do this. What you are actually asking for is a command that could be used at the SQL command prompt OR code that can be run in SQL.
You've also been told not to use a Linked Server for reading excel.
Have a look at this article for using dynamic sheet names Anupama Natarajan: Dynamic Sheet Name in SSIS Excel Spreadsheet imports[^]
Member 11586797 20-Feb-18 4:15am    
Thanks for replying.Yes, exactly that's what i want. but can i get a SQL script only for doing this. as i am not writing any SSIS package for it.
Because Using SSIS package would require a dll Interop to be included and this Interop will not work on my production server.

So it wud be so nice of you if i can get the sql script inly
CHill60 20-Feb-18 7:04am    
You could put that code into a stored procedure then call that from the sql prompt

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900