• Hansiehans - Friday, June 27, 2014 1:46 AM

    This is the code I used...enjoy 🙂
    -- Get table (worksheet) or column (field) listings from an excel spreadsheet

    DECLARE @linkedServerName sysname = 'TempExcelSpreadsheet'
    DECLARE @excelFileUrl nvarchar(1000) = 'D:\\SHARE\\WLAN\\Report.xlsx'

    IF OBJECT_ID('tempdb..#MyTempTable') IS NOT NULL
    DROP TABLE #MyTempTable;

    -- 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 info, each worksheet pbb gets its own unique name
    SELECT * INTO #MyTempTable FROM OPENROWSET('SQLNCLI', 'Server=(local);Trusted_Connection=yes;',
    'EXEC sp_tables_ex TempExcelSpreadsheet');

    SELECT TABLE_NAME
    FROM #MyTempTable

    --exec sp_executesql 'SELECT * INTO #MyTempTable FROM OPENROWSET(''SQLNCLI'', ''Server=(local);Trusted_Connection=yes;'',''EXEC sp_tables_ex TempExcelSpreadsheet'')'
    --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

    I've had this pinned as a link since you posted it and have been seriously remiss in saying "Thank You".  Great job (and hat's off to you for the embedded comments that make the code crystal clear) and thanks for sharing even years after the original questions was posted.  It's proof positive that all the reminders about how old a post is really don't matter and is definitely worth asking a follow up question or posting an answer even on old posts.  Well done and, even though you've not logged on for the previous two months, I hope you're still listening because I think this post has helped a whole lot of people that never took the time to say thanks for solving a fairly common and particularly itchy problem when you're the one that needs a solution.

    Shifting gears a bit, this would would make great "SQL Spackle" article.  You should write one and submit it.  If you want, I'd even be willing to proof it and do a technical review for you.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)