我试图从一个旧的Access数据库迁移一些数据到一个新的,一些列更改,例如旧的“地址”过去被分为街道,号码,城市和邮政编码现在只有一列。 所以我需要在更新新数据库之前将所有列合并为一个列。 我没有收到错误,也没有发出警告,尽管它只显示更新数据库的第一列。UNION ALL只返回第一列
我想要做的事:
//Open the connection with the database
oldBuys.Open()
newBuys.Open()
Dim codTable As New DataTable
Dim codTemp As Integer
Dim addressTable As New DataTable
Dim tempValue As String
//Command to find all the client codes on the new database
Dim findAllCodes As New OleDb.OleDbDataAdapter("SELECT Cod FROM Data", newBuys)
findAllCodes .Fill(codTable)
//Search for all the adress of that code
For i = 0 To codTable.Rows.Count - 1
codTemp = codTable.Rows(i).Item(0)
//Union all the values
Dim findAllAdress As New OleDb.OleDbCommand("SELECT Street FROM Data" & _
" WHERE [email protected] UNION ALL SELECT Number FROM Data WHERE [email protected] UNION ALL SELECT City" & _
" FROM Data WHERE [email protected] UNION ALL SELECT State FROM Data WHERE [email protected] UNION" & _
" ALL SELECT postalCode FROM Data WHERE [email protected]", cnCompraAntigo)
findAllAdress.Parameters.AddWithValue("@cod1", codTemp)
findAllAdress.Parameters.AddWithValue("@cod2", codTemp)
findAllAdress.Parameters.AddWithValue("@cod3", codTemp)
findAllAdress.Parameters.AddWithValue("@cod4", codTemp)
findAllAdress.Parameters.AddWithValue("@cod5", codTemp)
//Transfer the value to the variable
tempValue = findAllAdress.ExecuteScalar
//Try to update the new database
Dim updateAllAdress As New OleDb.OleDbCommand _
("UPDATE Data SET [email protected] WHERE [email protected]", newBuys)
updateAllAdress.Parameters.AddWithValue("@end", tempValue)
updateAllAdress.Parameters.AddWithValue("@cod", codTemp)
updateAllAdress.ExecuteNonQuery()
Next
newBuys.Close()
oldBuys.Close()
UNION用于组合来自不同查询的行,您希望将列组合起来,在这种情况下,您的查询应将它们连接起来。 – OTTA