ODBC is not an API I have worked with, so I'm not really in position to give advice. It does not help that your code sample is not coherent. You first declare and assign a variable query
and then there is a query2
. So even if I would know ODBC better, I might still be confused over what you are doing.
However, what you could try is to issue SET NOCOUNT ON before you running anything else. These "rows affected" messages often causes problems, if you don't cater for them. But this is just a shot in the dark.
Permit me also to point out on thing about the SQL as such. You have an INSERT statement without a column list. This is very bad practice in my opinion. If you or someone else innocently adds a nullable column to the table, thinking nothing can go wrong, your code will stop working. That is, the code should read:
INSERT tbl(col1, col2, ...)
SELECT ...