Access/SQL Append and Update Query

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.
LVL 10
victornegriAsked:
Who is Participating?
 
rockiroadsConnect With a Mentor Commented:
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

e.g.

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
     else
           'do update
     end if

     rs.movenext
loop



Does this logic make sense?

e.g. tables with values
linkedtable

part1,100
part2,200
part3,300

sqlserver

part1,lucas
part3,wimpey

the above sql will produce

part1,100,lucas
part2,200,<<null>>
part3,300,wimpey


0
 
rockiroadsCommented:
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
e.g.

update sqlservertable, linkedexceltabke
set sqlservertable.partfield1 = linkedexceltable.partfield1
where sqlservertable.keyfield1 = linkedexceltable.keyfield1
0
 
victornegriAuthor Commented:
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?
0
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
rockiroadsCommented:
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
0
 
LowfatspreadCommented:
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
...  

0
 
abd00nCommented:
Hi,
Check my simple procedure (I call it XL_Loader)

http://www.experts-exchange.com/Databases/Oracle/Q_21865533.html

If it is OK! then I can give some more hints!
0
 
victornegriAuthor Commented:
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?
0
 
LowfatspreadCommented:
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...


 hth

;-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.