Access/SQL Append and Update Query

Posted on 2006-06-02
Medium Priority
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
  • 3
  • 2
  • 2
  • +1
LVL 65

Expert Comment

ID: 16822035
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

ID: 16822500
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

ID: 16822546
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

LVL 50

Expert Comment

ID: 16822856
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


Expert Comment

ID: 16823274
Check my simple procedure (I call it XL_Loader)


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

Author Comment

ID: 16823554
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

rockiroads earned 2000 total points
ID: 16823588
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

ID: 16823948
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 a.pk = 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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
In this article, I’ll look at how you can use a backup to start a secondary instance for MongoDB.
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…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

840 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