简体   繁体   English

Excel for Mac VBA - SQL Server INSERT 语句用于 QueryTables

[英]Excel for Mac VBA - SQL Server INSERT statement for QueryTables

I have been working with Excel for Mac on a program that has tens of thousands of lines of code in VBA and is used across both Windows & Mac.我一直在使用 Excel for Mac 开发一个程序,该程序在 VBA 中有数万行代码,并在 Windows 和 Mac 中使用。 I am able to successfully import information into the program using the QueryTables object, but I cannot get a SQL INSERT statement to execute.我能够使用 QueryTables object 成功地将信息导入程序,但我无法执行 SQL INSERT 语句。 Is there any way to INSERT using QueryTables or is this the only way execute a command like this against the database?有没有办法使用 QueryTables 插入,或者这是对数据库执行这样的命令的唯一方法? I have downloaded the Actual ODBC drivers for SQL Server that are compatible, but have not used them at all.我已经下载了兼容的 SQL 服务器的实际 ODBC 驱动程序,但根本没有使用它们。

    If colInsertCustomers.Count > 0 Then
        sql = "INSERT INTO dbo.tblCustomersStage "
        sql = sql & cust.SQLFields
        sql = sql & " VALUES "
        For Each cust In colInsertCustomers
            sql = sql & cust.SQLValues & ","
        Next cust
        sql = Left(sql, Len(sql) - 1) ' Remove the last comma
        sql = sql & ";"
        qt.CommandText = sql
        qt.Refresh
    End If 

I have tested the SQL generated by the above code and the SQL string (sql) that it generates, works to put data into the database (using Azure Data Studio).我已经测试了上面代码生成的 SQL 和它生成的 SQL 字符串(sql),用于将数据放入数据库(使用 Z3A580F142203677F1F0BC30898F63F53)。 But every time that I get to qt.Refresh, it fails with the INSERT statement.但是每次我到达 qt.Refresh 时,它都会因 INSERT 语句而失败。

For clarification为了澄清

qt is a validly initialized QueryTable that has a functional connection to the database and is able to execute multiple SELECT statements against the database. qt是一个有效初始化的 QueryTable,它与数据库有功能连接,并且能够对数据库执行多个 SELECT 语句。

SQLFields returns the list of fields to INSERT including brackets & parentheses SQLFields将字段列表返回到 INSERT,包括括号和括号

SQLValues returns the list of values aligned with SQLFields including commas, appropriate tick marks, and parentheses SQLValues返回与 SQLFields 对齐的值列表,包括逗号、适当的刻度线和括号

cust is a CCustomer object that has too many properties and methods to list, but it is functional as well. cust 是一个 CCustomer object,它有太多的属性和方法无法列出,但它也很实用。

EDIT: Here is the connection string and definition of qt with obvious things obfuscated.编辑:这是 qt 的连接字符串和定义,其中明显的东西被混淆了。

Set qt = ws.ListObjects.Add(SourceType:=0, Source:= _
            "ODBC;DRIVER=SQL Server;" & _
            "SERVER=myserver.database.windows.net;" & _
            "Database=mydb;UID=$;", _
            Destination:=Range("$A$1")).QueryTable

EDIT 2: As a side-note, the Actual driver is able to connect, and execute these statements through Microsoft Query (Data Tab -> Get External Data -> SQL Server ODBC) without a problem.编辑2:作为旁注,实际驱动程序能够连接并通过Microsoft Query(数据选项卡->获取外部数据-> SQL服务器ODBC)执行这些语句而没有问题。 But when I try to record a macro while using this, I get nothing recorded.但是当我尝试在使用它时录制宏时,我什么也没记录。 The connection string created by Microsoft Query is: Microsoft Query 创建的连接字符串是:

DRIVER=SQL Server;SERVER=myserver.database.windows.net;UID=myname;PWD=mypwd;APP=Microsoft Excel;WSID=my-computer;DATABASE=mydb;

I determined through the course of study on this issue that it is not possible to advance an Insert Statement through the QueryTable object in VBA.我通过对这个问题的学习过程确定,不可能通过 VBA 中的 QueryTable object 推进插入语句。 Although MSQuery works to insert from within Excel Mac, it does not have an exposed VBA object to allow this manipulation.尽管 MSQuery 可以从 Excel Mac 中插入,但它没有暴露的 VBA object 允许这种操作。 I have chosen to move development to a Windows PC, I know that there are non-free solutions through two separate ODBC drivers that interact with SQL Server, this was one bridge too far for using the Mac (as much as I love it).我选择将开发转移到 Windows PC,我知道通过两个单独的 ODBC 驱动程序可以与 SQL 服务器交互,这是非免费的解决方案(我非常喜欢它)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM