Access/SQL Append and Update Query

Posted on 2006-06-02
Last Modified: 2008-01-09
I want to import data from an Excel spreadsheet to a SQL 2000 db via Access 2003. The data is going into a parts list and is updating the cost of existing parts plus adding new parts to the list. How would I create the Append/Update query in Access to do both functions? Or do I have to run two separate queries? 500 pt question because I'll probably have a few more questions after the fact.
Question by:victornegri
    LVL 65

    Expert Comment

    What u could do is to link in the Excel spreadsheet into Access2003

    u can do file/get external data/link
    or do it through code

    once in, u can now treat it as a table

    without knowing your tables/fields I dont know how to write the query

    but if there is a code in the linked xcel sheet, that links to parts list in sql server, then u could perhaps do it then

    update sqlservertable, linkedexceltabke
    set sqlservertable.partfield1 = linkedexceltable.partfield1
    where sqlservertable.keyfield1 = linkedexceltable.keyfield1
    LVL 10

    Author Comment

    So if I had some entries in linkedexceltable that did not exist in sqlservertable the update query would append them to the end of the sqlservertable? Or would I have to write another query?

    I.e. sqlsrevertable.keyfield1 has values ranging from 1 - 100. linkedexceltable.keyfield1 has values ranging from 90 - 200. With the query you mentioned above, 90-100 would get updated with new values because sqlservertable.keyfield1 = linkedexceltable.keyfield1. Would items 101 - 200 get added to the sqlservertable?
    LVL 65

    Expert Comment

    alright, I guess I miss the part of "adding new items"

    You will need another query to do that

    something along the lines of

    insert into sqlservertable (field1, field2, field3) select field1, field2, field3 from linkedtableexcel a where keyfield1 not in (select keyfield1 from sqlservertable where keyfield1 = a.keyfield1)

    obviously the examples only give one keyfield match, u need to do more of there are more keyfields

    the alternative is VBA

    but that more or less involves reading and checking tables
    LVL 50

    Expert Comment

    you will need to do and Insert and an Update eventually...

    i'd suggest import the spreadsheet into your access database...

    then validate the data... in access (i presume thats why you want to involve access anyway..)

    then  Insert your data into a Staging table on the SQL Server Database..  
    and then invoke a stored procedure on the sql server    database
    which will insert the additional rows from the staging table into the main table
    and do the required updates for the existing data ...
    and sort out you audit requirements...

    you don't say but you update actions should actually
    involve creation of new rows and then end dating of the existing rows rather than a direct replace
    so that your applications can select the appropriate values for the timeperiods they where applicable

    LVL 4

    Expert Comment

    Check my simple procedure (I call it XL_Loader)

    If it is OK! then I can give some more hints!
    LVL 10

    Author Comment

    rockiroads: could you (or anyone) give me an example on how I would do this using VBA?

    Lowfatspread: what are my alternatives if I don't use Access. BTW, There are 1000s of entries that need to be either updated or inserted. Could you give me an example of the TSQL commands I would use to accomplish this (excuse my ignorance). BTW, what you say at the end is true but 1) this is for an eCommerce site and all items in the tables appear on the website -- fixing this would require a lot more time than my client has. 2) My client doesn't care about saving the old values.

    abd00n: I'll try your suggestion out but what I was looking for was a solution where my client can essentially just click a button (or two) and everything works.

    Is there a way to do this in Access (crappy pseudocode to follow):

    If (linkedexceltable.keyfield1 = "") {
         Insert ...
         Write to log table
    Else {
         Append ...
         Write to log table

    or something to that extent?
    LVL 65

    Accepted Solution

    best thing is to do a left join

    select linkedtable.*, sqlserver.mandatorydatafield
    from linkedtable left join sqlservertable on linkedtable.keyfield1 = sqlservertable.keyfield1

    ensure u select a field which definitely has a value

    left join will pick all records in the linkedtable and select data from any matching rows in sqlserver

    then in vba u can do this

    sSql is a variable that holds the above query

    here is the logic

    open recordset, one would assume u use ADO here, are u familiar with that

    assuming u using adp


    dim conn as adodb.connection
    dim rs as adodb.recordset

        Set conn = Application.CurrentProject.Connection
        set rs = new adodb.recordset
        rs.activeconnection = conn
        rs.CursorType = adOpenKeyset
        rs.LockType = adLockPessimistic 'Invoke Pessimistic Locking
        rs.CursorLocation = adUseServer
        rs.Open "Select * from Imp"

    do while not rs.EOF
        'How to determine add or update - if sqlserver value is null, then its an add
         if isnull(rs!mandatorydatafield) = true then  
               'do add
               'do update
         end if


    Does this logic make sense?

    e.g. tables with values




    the above sql will produce


    LVL 50

    Expert Comment

    1) TRUNCATE / EMPTY your StagingTable
    2) load the Data to the staging table... e.g. via BCP or a BULK INSERT
    3) then execute the 2 following sql statements

    Update MainTable
       set cola = case when U.cola <> M.Cola then U.cola else M.cola end  
          .colb = case when U.colb <> M.Colb then U.colb else M.colb end
          ,  ... etc for each other column in table
      from Maintable as M
     Inner Join StagingTable as U
       on M.PK + U.PK
      and (
              (U.cola <> M.COla and not (U.cola is null and M.Cola is null)
           or (U.colb <> M.COlb and not (U.colb is null and M.Colb is null)      
           or ...

    insert into maintable
     (column list )
     select column list
       from StagingTable as A
      Where Not exists (select X.PK from Maintable as X
                            where = X.PK)
    4) its been updated...

    PK is the primary key of the Table..

    ps. If your client is not bothered about maintaining audit records then , they're not going to be inbusiness very
    long.. so ditch them or refer them to your local trading standards / taxation bodies...



    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Database tuning – How to start and what to tune. This question is frequently asked by many people, both online and offline. There is no hard and fast rule-of-thumb for performance tuning, however, before beginning the tuning process one should a…
    This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
    Video by: Steve
    Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    755 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

    21 Experts available now in Live!

    Get 1:1 Help Now