Solved

vb: updating records

Posted on 2004-09-06
6
232 Views
Last Modified: 2010-04-03
Hi,

I've got two tables with about 18000 records. I need to grab a value from TableA and push it into TableB (provided that TableA.ColumnB=TableB.ColumnB). This is what I've got so far:

Module:

Public Sub Conn_Open
 strInsert = "select * from tblCustomer"
strPurchase = "select * from tblPurchase"
With conn
.CommandTimeout = 150
.ConnectionString = ""
.ConnectionTimeout = 150
If conn.State = adStateClosed Then
conn.Open
End If
End With

With rstInsert
.CursorLocation = adUseServer
.CursorType = adOpenDynamic
.LockType = adLockOptimistic
If rstInsert.State = adStateClosed Then
rstInsert.Open strInsert, conn
End If
End With

With rstPurchase
.CursorLocation = adUseServer
.CursorType = adOpenDynamic
.LockType = adLockOptimistic
If rstPurchase.State = adStateClosed Then
rstPurchase.Open strPurchase, conn
End If
End With

End Sub

Form:

Private Sub Command1_Click()
modconn.Conn_Open
Do While Not rstPurchase.EOF
If rstInsert.EOF=True or rstInsert.BOF=True Then
    rstInsert.MoveFirst
End If
strI_Customers=rstInsert.Fields("Customer").Value
strI_CustNo=rstInsert.Fields("CustNo").Value
strP_CustNo=rstPurchase.Fields("CustNo").Value
  If strI_Customers=rstPurchase.Fields("Customer").Value and strI_CustNo=strP_CustNo Then
      rstInsert.MoveNext
      rstPurchase.MoveNext
 ElseIf strI_CustNo=strP_CustNo And IsNull(rstPurchase.Fields("Customer").Value) Then
      rstPurchase.Fields("Customer").Value=strI_Customers
      rstPurchase.Update
      rstPurchase.MoveNext
      rstInsert.MoveNext
 Else
     rstInsert.MoveNext
 End If
Loop
end sub

Now the code works and it does it's job, however it takes ages to update the Purchase table as it goes row by row.

Have you got any idea as to how to achieve the same in a faster way, as believe me it takes about 7h to update 18000records.

Any advice much appreciated.


Thanx
0
Comment
Question by:a_mihajlo
  • 3
  • 2
6 Comments
 
LVL 4

Expert Comment

by:jonnietexas
ID: 11993481
Icky.  Managing data integrity through code blows. Use SQL to do it.  Now, I'm an oracle jockey so it might be a bit different with syntax but hopefully you'll get the idea.

update tableb b1 set b1.value =  (select a.value from tablea a, tableb b
where a.ColumnB=b.ColumnB
and b.key = b1.key)

The difference it that you're doing a full table scan of the 18000 rows every time you look at a row so really 18000*18000 and that's a lot.
0
 
LVL 11

Accepted Solution

by:
coopzz earned 500 total points
ID: 12004169
I believe this may be what your after

before you try Clarify: this query should update tblPurchase.Customer column when CustNo are equal and Customer are not equal

UPDATE tblPurchase
      SET tblPurchase.Customer = tblCustomer.Customer
      FROM tblPurchase, tblCustomer
      WHERE tblPurchase.CustNo = tblCustomer.CustNo AND tblPurchase.Customer <> tblCustomer.Customer

I haven't tested it but the Syntax checks out in a query analyser  as with all big updating querys ##NOTE: USE WITH CAUTION.. Make sure you have backups.

the idea is taken from T-sql reference below
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_ua-uz_82n9.asp
the example.
C. Use the UPDATE statement using information from another table
This example modifies the ytd_sales column in the titles table to reflect the most recent sales recorded in the sales table.

UPDATE titles
   SET ytd_sales = titles.ytd_sales + sales.qty
      FROM titles, sales
         WHERE titles.title_id = sales.title_id
         AND sales.ord_date = (SELECT MAX(sales.ord_date) FROM sales)
0
 
LVL 11

Expert Comment

by:coopzz
ID: 12098231
how'd you go  .. any luck ??
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:a_mihajlo
ID: 12198816
I fixed it with this sql string:

strSql="update TableB set TableB.ColumnA=TableA.ColumnA from TableA, TableB where TableA.ColumnB=TableB.ColumnB"

And now it works like magic!!!!

Thanx a lot for all the valuable comments you guys have posted.... :-))))
0
 
LVL 11

Expert Comment

by:coopzz
ID: 12199282
so ya gonna close the question then.

glad to help
0
 

Author Comment

by:a_mihajlo
ID: 12199328
thanx again
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

I have written before on the benefits of using a Boot media other than your HDD when it has become infected.   The article I wrote about creating a bootable CD/DVD/USB (http://e-e.com/A_2343.html) was mainly concerned with building a UBCD4Win on CD …
Lets start to have a small explanation what is VAAI(vStorage API for Array Integration ) and what are the benefits using it. VAAI is an API framework in VMware that enable some Storage tasks. It first presented in ESXi 4.1, but only after 5.x sup…
This tutorial will walk an individual through the process of installing the necessary services and then configuring a Windows Server 2012 system as an iSCSI target. To install the necessary roles, go to Server Manager, and select Add Roles and Featu…
This Micro Tutorial will teach you how to reformat your flash drive. Sometimes your flash drive may have issues carrying files so this will completely restore it to manufacturing settings. Make sure to backup all files before reformatting. This w…

762 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

24 Experts available now in Live!

Get 1:1 Help Now