Click here to Skip to main content
15,886,796 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two tables A and B shown.
Table A
HoleNo Depth_from Depth_to
A1 1 2
A1 3 4
A1 5 6

Table B
HoleNo Depth_from Depth_to
A1
A1
A1

Now How do I update table B so that it looks like table A
Note that no changes must be made to table A.
I have tried to assign id's to table B so that I do the update based on the unique id's, but since I am not to make any changes to table A I am finding it very difficult.

this is my code
SQL
I first save all HoleNo into B from A
sqlSTR = "SELECT * FROM A"
ExecuteSQLQuery(sqlSTR) 'call function to execute the query
For Each row1 As DataRow In sqlDT.Rows

sqlSTR = "INSERT INTO B (HoleNo) VALUES ('" & row1(0) & "')"
ExecuteSQLQuerylocal(sqlSTR) 'call function to execute the query

Next

So after inserting all HoleNo then I try to update the rest of the fields with this code

sqlSTR = "SELECT * FROM A"
ExecuteSQLQuery(sqlSTR) 'call function to execute the query
For Each row1 As DataRow In sqlDT.Rows

sqlSTR = "UPDATE B SET Depth_from ='" & row(0) & "' WHERE HoleNo ='" & row(0) & "'"
ExecuteSQLQuerylocal(sqlSTR)

Next
Posted
Updated 27-Feb-13 23:17pm
v2
Comments
Prakash Thirumoorthy 28-Feb-13 3:14am    
where is your query which was u tried?
wizy@2020 28-Feb-13 5:03am    
I first save all HoleNo into B from A
sqlSTR = "SELECT * FROM A"
ExecuteSQLQuery(sqlSTR) 'call function to execute the query
For Each row1 As DataRow In sqlDT.Rows

sqlSTR = "INSERT INTO (B) VALUES ('" & row1(0) & "')"
ExecuteSQLQuerylocal(sqlSTR) 'call function to execute the query

Next

then I update table B using this
sqlSTR = "SELECT * FROM A"
ExecuteSQLQuery(sqlSTR) 'call function to execute the query
For Each row1 As DataRow In sqlDT.Rows

sqlSTR = "UPDATE B SET Depth_from='" & row1("Depth_from") & "' WHERE HOLE_NUMBER='" & row(0) & "'"
ExecuteSQLQuerylocal(sqlSTR) 'call function to execute the query

Next
wizy@2020 28-Feb-13 5:08am    
I first save all HoleNo into B from A
sqlSTR = "SELECT * FROM A"
ExecuteSQLQuery(sqlSTR) 'call function to execute the query
For Each row1 As DataRow In sqlDT.Rows

sqlSTR = "INSERT INTO B (HoleNo) VALUES ('" & row1(0) & "')"
ExecuteSQLQuerylocal(sqlSTR) 'call function to execute the query

Next

So after inserting all HoleNo then I try to update the rest of the fields with this code

sqlSTR = "SELECT * FROM A"
ExecuteSQLQuery(sqlSTR) 'call function to execute the query
For Each row1 As DataRow In sqlDT.Rows

sqlSTR = "UPDATE B SET Depth_from ='" & row(0) & "' WHERE HoleNo ='" & row(0) & "'"
ExecuteSQLQuerylocal(sqlSTR)

Next

1 solution

Delete Table B from Database
then Create Table B from Table A

Use Sql Query
SQL
     DROP TABLE B
GO
     SELECT * INTO B From A
GO
 
Share this answer
 
Comments
wizy@2020 28-Feb-13 5:17am    
I first save all HoleNo into B from A
sqlSTR = "SELECT * FROM A"
ExecuteSQLQuery(sqlSTR) 'call function to execute the query
For Each row1 As DataRow In sqlDT.Rows

sqlSTR = "INSERT INTO B (HoleNo) VALUES ('" & row1(0) & "')"
ExecuteSQLQuerylocal(sqlSTR) 'call function to execute the query

Next

So after inserting all HoleNo then I try to update the rest of the fields with this code

sqlSTR = "SELECT * FROM A"
ExecuteSQLQuery(sqlSTR) 'call function to execute the query
For Each row1 As DataRow In sqlDT.Rows

sqlSTR = "UPDATE B SET Depth_from ='" & row(0) & "' WHERE HoleNo ='" & row(0) & "'"
ExecuteSQLQuerylocal(sqlSTR)

Next

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900