Match SQL ODBC Results on distinct ID; insert/update/archive on status

  • All -- I have searched all 37 pages of SQL/EXCEL questions and cannot find the distinct issue I am trying to solve for. It is to UPDATE/INSERT/ARCHIVE(MOVE) rows in Excel workbook based on SQL results. I currently have a functioning MSSQL 2008r2 ODBC workbook connection within an Excel 2010 worksheet which pulls back roughly 38 columns. I need the following to occur:


    Based on the SQL results returned lookup for a match to a DistinctID (B:B) in worksheet OPEN

    • IF DistinctID NOT EXIST, THEN insert all new values across 38 columns in worksheet OPEN
    • IF DistinctID EXIST and STATUS column = 'CLOSED' (from a previous value not 'CLOSED'), THEN UPDATE column all changed values and MOVE row to separate worksheet named CLOSED
    • IF DistinctID EXIST and STATUS column <> 'CLOSED' and ANY column values have changed, THEN UPDATE column values in same worksheet leaving row in worksheet OPEN
    • IF DistinctID EXIST and all values are unchanged, THEN no update to row in worksheet OPEN

    I have a macro which runs and copies original data and inserts new, but it is proving to be of little value.

  • Re: Match SQL ODBC Results on distinct ID; insert/update/archive on status


    SQL LOGIC pulling distinct Ticket_Nbr (which is the DISTINCTID I am speaking of)

    SQL
    SELECT 
        distinct(wo.WORKORDERID)                                    AS 'Ticket_Nbr'
         .............. 'REMOVED FROM LOGIC SINCE IRRELEVANT'
        ,std.STATUSNAME                                        AS 'Status'
         .............. 'REMOVED FROM LOGIC SINCE IRRELEVANT'
    FROM 
         .............. 'REMOVED FROM LOGIC SINCE IRRELEVANT'
    GROUP BY 
         .............. 'REMOVED FROM LOGIC SINCE IRRELEVANT'
     ORDER BY wo.WORKORDERID desc



    Macro for generic copy of data set prior to refresh

    Code
    Private Sub Worksheet_Change(ByVal Target As Range)    If Not Intersect(Target, Range("A1:AK500")) Is Nothing Then
            Application.EnableEvents = False
            Range("BA1:BK500").Copy Range("CA1:CK500")
            Range("A1:AK500").Copy Range("BA1:BK500")
            Application.EnableEvents = True
        End If
    End Sub
  • Re: Match SQL ODBC Results on distinct ID; insert/update/archive on status


    If you're using SQL 2008R2 why not load the data into a staging table and then perform a merge statement in t-sql?

  • Re: Match SQL ODBC Results on distinct ID; insert/update/archive on status


    I dont need to merge any data. I need to be able to update the spreadsheet when changes occur based on ID and STATUS. I need not update any SQL tables.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!