I am trying to to use the SELECT INTO statement to copy data from 2 sql server tables to 1 access table using vb .net 2005. This statement workes great to quickly copy 1 table to 1 table, but I am having issues with my Joins. I have a an Orders_tbl and Order_Details_tbl in sql server and want to copy select Order_Details_tbl records based off the status of the Orders in the Orders_tbl. So I would like to copy all the Order_Details_tbl records whose parent Orders_tbl record has a status of "ORDERED". Here is the code that I am trying:
Dim
oConn As ADODB.ConnectionoConn = New ADODB.Connection
oConn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & txtPath.Text & "\" & cmbPlant.Text & Format(Now.Date, "MMddyy") & ".mdb;Jet OLEDB:Database Password=smjmpm103;")
Dim sSQL As String
sSQL =
"SELECT Orders_tbl.OrderNumber, Order_Details_tbl.AutoOrderDetail INTO Order_Details_Backup_tbl FROM "sSQL = sSQL &
"[odbc;Driver={SQL Server};Server=" & sqllocation & ";Database=" & sqlname & ";Uid=" & sqlun & ";Pwd=" & sqlpw & ";].Orders_tbl INNER JOIN Order_Details_tbl ON Order_Details_tbl.AutoOrder = Orders_tbl.OrderNumber WHERE (Orders_tbl.Status = 'ORDERED')"oConn.Execute(sSQL)
I am getting a "Type mismatch in expression" exception. Any suggestions would be greatly appreciated.
jmayerPosted Dec 13, 2007, 9:31 AM
I double checked the connection string, and the ";" is needed after the pw, at least it works when I run:
sSQL =
"SELECT * INTO Orders_tbl FROM "sSQL = sSQL &
"[odbc;Driver={SQL Server};Server=" & sqllocation & ";Database=" & sqlname & ";Uid=" & sqlun & ";Pwd=" & sqlpw & ";].Orders_tbl WHERE Status = 'ORDERED'"oConn.Execute(sSQL)
There is something that I am missing in the join. I would think it could be done. I also tried aliasing as you suggested with the same exception thrown as previous.
Jan MontanoPosted Dec 11, 2007, 9:25 PM
My Bad. I overlooked that one. My initial reaction to the type mismatch was with the columns. Sorry.
Honestly, I haven't yet tried what you're doing. And I can't see anything wrong with the statement.
I just noticed in your odbc;driver string ...
[odbc;Driver={SQL Server};Server=" & sqllocation & ";Database=" & sqlname & ";Uid=" & sqlun & ";Pwd=" & sqlpw & ";]
If you remove the semicolon (;) just right before the closing bracket ( ] ), does the error still occur? As far as I can remember, it could be removed.
I'm not sure if your succeeding references to Orders_tbl (like the one in the where clause) refers to the same odbc driver string
So try using an alias just to be sure...
sSQL = "SELECT o.OrderNumber, d.AutoOrderDetail INTO Order_Details_Backup_tbl FROM "
sSQL = sSQL & "[odbc;Driver={SQL Server};Server=" & sqllocation & ";Database=" & sqlname & ";Uid=" & sqlun & ";Pwd=" & sqlpw & ";].Orders_tbl o INNER JOIN Order_Details_tbl d ON d.AutoOrder = o.OrderNumber WHERE (o.Status = 'ORDERED')"
Regards,
Jan
jmayerPosted Dec 11, 2007, 8:41 AM
Thank You for the reply.
From my understanding the SELECT INTO statement will create the Order_Details_Backup_tbl with the same columns and data types from the source tables, which in this example are both big int. The INSERT INTO statement would need the table already created and it would then append the records to that table. At least this is how I see it working when I have no joins. The SELECT * INTO will copy and create the exact column structure and data types in the destiation table. Ideally all I really want is all the Order_Details_tbl columns copied to the Order_Details_Backup_tbl, but because I only want the order details of the orders that have a status of "ORDERED", I need a relationship column, so I have added Orders_tbl.OrderNumber to the query. Maybe I am goig about it wrong.
Jan MontanoPosted Dec 10, 2007, 11:27 PM
How many columns does Order_Details_Backup_tbl have? Does it only have 2 columns matching Orders_tbl.OrderNumber and Order_Details_tbl.AutoOrderDetail column data types?
What is the data type for Orders_tbl.OrderNumber and Order_Details_tbl.AutoOrderDetail? What is the data type of the columns of your Order_Details_Backup_tbl?
Regards,
Jan