[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How to update column in TableA based on matching IDs in TableB

Posted on 2011-02-13
24
Medium Priority
?
476 Views
Last Modified: 2012-05-11
Hello,

TableA contains:

ID         ITEM               COUNTRY
10411    ItemNameA
10412    ItemNmaeB

TableB contains

ID         COUNTRY

10411   BEL,CAN,USA
10412   FRA,GBR,NLD

How do you loop through TableA add COUNTRY data  from TableB based on matching IDs from both tables?

ID         ITEM               COUNTRY
10411    ItemNameA     BEL,CAN,USA
10412    ItemNmaeB     FRA,GBR,NLD

Thanks,

Victor
0
Comment
Question by:vcharles
[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
  • 12
  • 6
  • 5
  • +1
24 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34884050
Try this SQL

Update TableA TA Set Country=(Select Country From TableB Where ID=TA.ID)
0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 34884054
It should be something like:

UPDATE TableA
SET COUNTRY = (SELECT TableB.COUNTRY FROM TableB WHERE TableB.ID = TableA.ID)
0
 

Author Comment

by:vcharles
ID: 34884148
Thanks for the code, will try them when I return home later.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 34884570
Update TableA Inner Join TableB
On TableA.ID=TableB.ID
Set TableA.Country=TableB.Country
0
 

Author Comment

by:vcharles
ID: 34885232
Hello,

I tried the code below, but the receive the following error: Operation must use an updateable query.
on line da.Fill(dt1).


  Dim ConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\aopt2002org.mdb;Persist Security Info=True;Jet OLEDB:Database Password=test"
        Dim da As New OleDb.OleDbDataAdapter("Update AOP6A AOP5 Set CA2=(Select Country From AOP5 Where SN=AOP5.SN)", ConnectionString)
        objConnection.Open()
        Dim dt1 As New DataSet
        da.Fill(dt1)
        da.Update(dt1)

Thanks,
Victor
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 34885247
post the actual names of the tables and field names
0
 

Author Comment

by:vcharles
ID: 34885325
Tables: AOP6A, AOP5
FIELDS: CA2          FOR AOP6A
             Country    FOR AOP5
I need to separate the countries in CA2 by a ",", for example BEL,CAN,FRA,USA

Thanks,

Victor            
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 34885347
Update AOP6A Inner Join AOP5
On AOP6A.ID=AOP5.ID
Set AOP6A.CA2=AOP5.Country
0
 

Author Comment

by:vcharles
ID: 34885440
I am trying your code but I'm receiving the following error on line da.Fill(dt1):
Syntax error (missing operator) in query expression 'AOP6A.ID=AOP5.ID AOP6A.CA2 = AOP5.Country'.

Code:

 Dim ConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\aopt2002org.mdb;Persist Security Info=True;Jet OLEDB:Database Password=testaopupdate"
             Dim da As New OleDb.OleDbDataAdapter("Update AOP6A Inner Join AOP5 On AOP6A.ID=AOP5.ID AOP6A.CA2 = AOP5.Country)", ConnectionString)
        objConnection.Open()
        Dim dt1 As New DataSet
        da.Fill(dt1)
        da.Update(dt1)
        dt1.Tables(0).AcceptChanges()
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 34885446
vcharles,
copy the whole statement
0
 

Author Comment

by:vcharles
ID: 34885467
Hello,

Below is the full statement

Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Dim ConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\aopt2002org.mdb;Persist Security Info=True;Jet OLEDB:Database Password=testaopupdate"
        ' Dim da As New OleDb.OleDbDataAdapter("Update AOP6A AOP5 Set CA2=(Select Country From AOP5 Where SN=AOP5.SN)", ConnectionString)

        Dim da As New OleDb.OleDbDataAdapter("Update AOP6A Inner Join AOP5 On AOP6A.ID=AOP5.ID AOP6A.CA2 = AOP5.Country)", ConnectionString)
        objConnection.Open()
        Dim dt1 As New DataSet
        'da.Fill(dt1)
        da.Fill(dt1)
        da.Update(dt1)
        dt1.Tables(0).AcceptChanges()
    End Sub
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 34885477
vcharles, this is what i posted

Update AOP6A Inner Join AOP5 On AOP6A.ID=AOP5.ID Set AOP6A.CA2=AOP5.Country

Open in new window

can't you see which one you omitted?

0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 34885494



        Dim da As New OleDb.OleDbDataAdapter("Update AOP6A Inner Join AOP5 On AOP6A.ID=AOP5.ID Set AOP6A.CA2 = AOP5.Country", ConnectionString)

Open in new window

0
 

Author Comment

by:vcharles
ID: 34885527
Thanks, will let you know how it works tomorrow.

Victor
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34887102
Dont use Fill. Fill works with Select query not Update. Try this code



Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
Dim ConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\aopt2002org.mdb;Persist Security Info=True;Jet OLEDB:Database Password=testaopupdate"

Dim dbcon As New OleDbConnection(connectionstring)
dbcon.Open
Dim dbcmd As New OleDbCommand
dbcmd.Connection = dbcon
dbcmd.CommandText = "Update AOP6A AOP5 Set CA2=(Select Country From AOP5 Where SN=AOP5.SN)"
dbcmd.ExecuteNonQuery()
dbcmd.Dispose
dbcon.Close
dbcon.Dispose

End Sub

Open in new window

0
 

Author Comment

by:vcharles
ID: 34887220
Hello,

I tried the code from the last post, but I received the folowing error:

Operation must use an updateable query.

on line:

dbcmd.ExecuteNonQuery()

Thanks,

Victor
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 2000 total points
ID: 34887242
Ok. Let's try again



Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
Dim ConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\aopt2002org.mdb;Persist Security Info=True;Jet OLEDB:Database Password=testaopupdate"

Dim dbcon As New OleDbConnection(connectionstring)
dbcon.Open
Dim dbcmd As New OleDbCommand
dbcmd.Connection = dbcon
dbcmd.SelectCommand = "Select * From AOP5"
Dim dTable As New DataTable
Dim dbadp As New OleDbDataAdapter(dbcmd)
dbadp.Fill(dTable)

For i As Integer = 0 to dTable.Rows.Count - 1
   dbcmd.CommandText = "Update AOP6A Set CA2='" & dTable.Rows(i).Item(1) & "' Where ID=" & dTable.Rows(i).Item(0)
   dbcmd.ExecuteNonQuery()
Next
dTable.Dispose
dbcmd.Dispose
dbcon.Close
dbcon.Dispose

End Sub

Open in new window

0
 

Author Comment

by:vcharles
ID: 34887305
Do I need to import something? I'm getting an error on dbcmd.SelectCommand (Error: SelectCommand is not a member of System.Data.OleDb.OleDbCommand)
0
 

Author Comment

by:vcharles
ID: 34887340
I am getting Data type mismatch in criteria expression.error. The  ID in your code was replaced by SN which is a text field, could that be the problem?

Dim ConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\aopt2002org.mdb;Persist Security Info=True;Jet OLEDB:Database Password=testaopupdate"
        Dim dbcon As New OleDbConnection(ConnectionString)
        dbcon.Open()
        Dim dbcmd As New OleDbCommand
        dbcmd.Connection = dbcon
        dbcmd.CommandText = "Select * From AOP5"
        Dim dTable As New DataTable
        Dim dbadp As New OleDbDataAdapter(dbcmd)
        dbadp.Fill(dTable)

        For i As Integer = 0 To dTable.Rows.Count - 1
            dbcmd.CommandText = "Update AOP6A Set CA2='" & dTable.Rows(i).Item(1) & "' Where SN=" & dTable.Rows(i).Item(0)
            dbcmd.ExecuteNonQuery()
        Next
        dTable.Dispose()
        dbcmd.Dispose()
        dbcon.Close()
        dbcon.Dispose()
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34887713
If its a Text field, change

dbcmd.CommandText = "Update AOP6A Set CA2='" & dTable.Rows(i).Item(1) & "' Where SN=" & dTable.Rows(i).Item(0)


to

dbcmd.CommandText = "Update AOP6A Set CA2='" & dTable.Rows(i).Item(1) & "' Where SN='" & dTable.Rows(i).Item(0) & "'"
0
 

Author Comment

by:vcharles
ID: 34887937
It worked! but I'm confused. I have another column COUNTRYALL in AOP5 which contains a string of countries:BEL,CAN,FRA,USA etc..

When I replace the code with the one below, countryall is the 11th column in the table.

dbcmd.CommandText = "Update AOP6A Set CA2='" & dTable.Rows(i).Item(11) & "' Where SN='" & dTable.Rows(i).Item(0) & "'"

It doesn't copy the countryall data to the CA2 table in AOP6A, How do I choose another column in AOP5 to populate CA2?

Thanks,

Victor
0
 

Author Comment

by:vcharles
ID: 34887995
Please disregard my last post.

It's working great!

Thank You very much.

Victor
0
 

Author Closing Comment

by:vcharles
ID: 34888000
Thank You!
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34895398
Glad to help :-)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

656 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