[英]How to dump data from SQL Server into MS Access table using hybrid connections
I need to pull data from SQL server using a query where its statement is stored in a MS access table and dump the data into another table.我需要使用查询从 SQL 服务器中提取数据,其中的语句存储在 MS 访问表中,并将数据转储到另一个表中。
There are 2 tables, 01-MyStoredSQLs
and 02-tmpTableData
有 2 个表,
01-MyStoredSQLs
和02-tmpTableData
The problem is that there are 2 connections as well.问题是也有 2 个连接。 That's why I called it Hybrid.
这就是为什么我称之为混合。
My Final result is the data from SQL Server into the tmpTableData
(result of a query where its statement is stored in the MyStoredSQLs table)我的最终结果是从 SQL Server 到
tmpTableData
的数据(查询的结果,其语句存储在 MyStoredSQLs 表中)
Public Sub DumpSQLServerData()
Dim conn As ADODB.Connection
On Error GoTo errMSG
Set conn = New ADODB.Connection
conn.ConnectionString = "DSN=myDSN; UID=MyID;Pwd=MyPWd"
conn.Open
Dim db As DAO.Database
Set db = CurrentDb
Dim rs As DAO.Recordset
'Retrieving the SQL Statment that is stored in the MyStoredSQLs table
Set rs = db.OpenRecordset("SELECT [Statement] FROM MyStoredSQLs WHERE ID=1")
Dim tmpSQL As String
Dim tmpINTOSQL As String
tmpSQL = rs(0)
'Hybrid statement
tmpINTOSQL = "INSERT INTO tmpTableData" & tmpSQL
conn.Execute (tmpINTOSQL), dbFailOnError
conn.Close
rs.Close
db.Close
Set conn = Nothing
Set rs = Nothing
Set db = Nothing
errMSG:
Debug.Print Err.Description
End Sub
You can't expect SQL server to magically be able to access tables stored elsewhere.您不能指望 SQL Server 能够神奇地访问存储在其他地方的表。
Execute the query in Access, and either use a linked table, or specify the location of the table, eg在 Access 中执行查询,或者使用链接表,或者指定表的位置,例如
INSERT INTO tmpTableData SELECT Something FROM [ODBC;DSN=myDSN;UID=MyID;Pwd=MyPWd].Schema.TableInSQLServer
If you want to execute the query on SQL server, not Microsoft Access, then SQL server will need to be able to access the Access database file, and use OPENROWSET
to query the Access table.如果要在 SQL Server 上执行查询,而不是 Microsoft Access,则 SQL Server 需要能够访问 Access 数据库文件,并使用
OPENROWSET
查询 Access 表。
As an alternate, you can create a passthrough query, and use that to copy the table:作为替代方案,您可以创建一个直通查询,并使用它来复制表:
Dim qd As DAO.QueryDef
Set qd = db.CreateQueryDef("~tmpQuery")
qd.Connect = "ODBC;DSN=myDSN;UID=MyID;Pwd=MyPWd"
qd.ReturnsRecords = True
qd.SQL = tmpSQL
Set qd = Nothing
db.Execute "INSERT INTO tmpTable SELECT * FROM [~tmpQuery]"
db.QueryDefs.Delete "~tmpQuery"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.