Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

vb: updating records

Posted on 2004-09-06
6
Medium Priority
?
241 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 2000 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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 

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 Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

The article will include the best Data Recovery Tools along with their Features, Capabilities, and their Download Links. Hope you’ll enjoy it and will choose the one as required by you.
Many businesses neglect disaster recovery and treat it as an after-thought. I can tell you first hand that data will be lost, hard drives die, servers will be hacked, and careless (or malicious) employees can ruin your data.
This video teaches viewers how to encrypt an external drive that requires a password to read and edit the drive. All tasks are done in Disk Utility. Plug in the external drive you wish to encrypt: Make sure all previous data on the drive has been …
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…
Suggested Courses

926 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