Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

vb: updating records

Posted on 2004-09-06
6
Medium Priority
?
240 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 

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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Concerto Cloud Services, a provider of fully managed private, public and hybrid cloud solutions, announced today it was named to the 20 Coolest Cloud Infrastructure Vendors Of The 2017 Cloud  (http://www.concertocloud.com/about/in-the-news/2017/02/0…
Windows Server 2003 introduced persistent Volume Shadow Copies and made 2003 a must-do upgrade.  Since then, it's been a must-implement feature for all servers doing any kind of file sharing.
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

715 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