how can i refresh a pivot table using tsql 2000

Posted on 2009-04-21
Last Modified: 2012-05-06
how can i refresh a pivot table using tsql 2000. I know it can be done using the oacreate stored proc or similar but im after some guidance or useful code.
Question by:9772885
    LVL 14

    Expert Comment

    Refresh a pivot table? You mean in Excel?

    Or you want to have a query which returns a set of results with dynamic columns?

    LVL 51

    Expert Comment

    by:Mark Wills
    Did exactly this not so long ago using OA procedures. Ended up being part of a knowledge base SQL User group website (italian), should be easy enough to follow :

    LVL 51

    Accepted Solution

    OK, maybe this will make it easier....

    Simply create a folder C:\ee
    and save the attached spreadsheet exactly as is into that folder.

    Open it have a look at the work book - two worksheets 1st is the Pivot, 2nd is the data source for that pivot.

    You will see a value of 22222.22 in Feb in the Pivot table. We will change that value on the source data and then refresh that pivot.

    The Pivot could easily be directly off a database (in which case you just need the refresh part, not the changing of the value).

    NOW CLOSE THE WORKBOOK. otherwise you might get a sharing violation.

    Then run the code below, and re-open the spreadsheet.

    DECLARE @FileName varchar(512), 
            @status int,
            @Excel int,
            @WorkBook int,
            @WorkSheet int,
            @WorksheetIndex int,
            @ErrorMessage varchar(255),
            @editing int
    SET @filename = 'c:\ee\pivot-example.xls'        -- excel filename
    SET @editing = 0
    SET @ErrorMessage = 'Trying to Update Spreadsheet ' + @filename          -- of course you should put more controls in...
    EXEC @status = sp_OACreate 'Excel.Application', @Excel output
    IF @status=0 EXEC @status = sp_OAMethod @Excel, 'WorkBooks.Open', @WorkBook output, @FileName
    IF @status=0 
        SET @editing = 1
        IF @status=0 EXEC @status = sp_OAMethod @WorkBook, 'Worksheets.Item', @WorkSheet output, 2      --change data on data worksheet - which is sheet 2
        IF @status=0 EXEC @status = sp_OAMethod @WorkSheet, 'Activate'
        IF @status=0 EXEC @status = sp_OASetProperty @WorkSheet, 'Range("D3:D3").font.bold', 1 
        IF @status=0 EXEC @status = sp_OASetProperty @WorkSheet, 'Range("D3").Value', '=21212.12'
    IF @status=0 
    and @editing = 1
        SET @editing = 2
        IF @status=0 EXEC @status = sp_OAMethod @WorkBook, 'Worksheets.Item', @WorkSheet output, 1      --update pivot on pivot worksheet - which is sheet 1
        IF @status=0 EXEC @status = sp_OAMethod @WorkSheet, 'Activate'
        IF @status=0 EXEC @status = sp_OAMethod @WorkSheet, 'PivotTables(1).RefreshTable'
        IF @status=0 EXEC @status = sp_OAMethod @WorkSheet, 'PivotTables(1).SaveData'
    IF @editing > 0
        EXEC sp_OAMethod @Excel, 'ActiveWorkbook.Save'
        EXEC sp_OAMethod @Excel, 'Workbooks.Close'
    EXEC sp_OAMethod @Excel, 'Close' 
    IF @status <> 0
        set @errormessage = @errormessage + '  Status Code =' +convert(varchar,@status) 
        raiserror (@ErrorMessage, 16, 1)
    EXEC sp_OADestroy @Excel
    EXEC sp_OADestroy @WorkSheet
    EXEC sp_OADestroy @WorkBook

    Open in new window


    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    This is my first ever Article on EE or elsewhere; therefore, please bear with me if I have some discrepancies in my writing. I read many articles and questions related to "how to pass values to SSIS packages at run-time?"  Hence, this common ques…
    The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
    Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
    Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

    746 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

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now