具有计数字段的可更新记录集 - MS Access

发布于 2024-11-08 19:55:50 字数 265 浏览 0 评论 0原文

我不是 SQL 专家。也许我在这里尝试做的事情根本不可能实现。

我正在尝试获取一个可更新的记录集,其中包含一个聚合函数结果的字段。

我正在寻找这样的东西:

SELECT Contact.*, Count(OrderID) as CountOfOrders
FROM Contact INNER JOIN Order ON Order.ContactID = Contact.ContactID
WHERE ContactID = 1

I'm not an SQL Expert. Maybe what I'm trying to do here isn't even possible.

I'm trying to get an updateable recordset that includes a field that is the result of an aggregate function.

I'm looking for something like this:

SELECT Contact.*, Count(OrderID) as CountOfOrders
FROM Contact INNER JOIN Order ON Order.ContactID = Contact.ContactID
WHERE ContactID = 1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独自唱情﹋歌 2024-11-15 19:55:50

当使用 MSDataShape OLE DE 提供程序 Jet(或 ACE)的 OLE DB 提供程序时,确实可以创建可更新的 ADO 记录集并APPEND基于一个集合函数,例如 COUNT()。生成的 SQL 式代码看起来更像是这样:

 SHAPE  {SELECT ContactID, ContactName FROM Contact} 
APPEND ({SELECT ContactID, OrderID FROM Orders} 
         RELATE ContactID TO ContactID
       ) As rsDetails, COUNT(rsDetails.OrderID) AS CountOfOrder

这是一个简短的“概念证明”:将以下内容粘贴到任何 VBA 模块中(例如使用 Excel),无需引用,在临时目录中创建一个新的 .mdb,创建包含数据的表,为了证明记录集是可更新的,ContactName 值已更改,并且记录集重新打开以显示它确实已更改:

Sub ShapeAppendCount()

  On Error Resume Next
  Kill Environ$("temp") & "\DropMe.mdb"
  On Error GoTo 0

  Dim cat
  Set cat = CreateObject("ADOX.Catalog")
  With cat
    .Create _
        "Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source=" & _
        Environ$("temp") & "\DropMe.mdb"

    Dim jeng
    Set jeng = CreateObject("JRO.JetEngine")
    jeng.RefreshCache .ActiveConnection

    Set .ActiveConnection = Nothing
  End With

  Dim con
  Set con = CreateObject("ADODB.Connection")
  With con
    .ConnectionString = _
        "Provider=MSDataShape;" & _
        "Data Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source=" & _
        Environ$("temp") & "\DropMe.mdb"
    .CursorLocation = 3
    .Open

    .Execute _
    "CREATE TABLE Contact (" & _
    "ContactID INTEGER NOT NULL UNIQUE, " & _
    "ContactName VARCHAR(20) NOT NULL);"

    .Execute _
    "CREATE TABLE Orders (" & _
    "ContactID INTEGER NOT NULL REFERENCES Contact (ContactID), " & _
    "OrderID INTEGER NOT NULL UNIQUE);"

    .Execute _
    "INSERT INTO Contact (ContactID, ContactName)" & _
    " VALUES (1, 'OneDayWhen');"

    .Execute _
    "INSERT INTO Orders (ContactID, OrderID)" & _
    " VALUES (1, 1);"

    .Execute _
    "INSERT INTO Orders (ContactID, OrderID)" & _
    " VALUES (1, 2);"

    Dim rs
    Set rs = CreateObject("ADODB.Recordset")
    With rs
      .CursorType = 2  ' adOpenDynamic
      .LockType = 4  ' adLockBatchOptimistic

      .Source = _
      " SHAPE {SELECT ContactID, ContactName FROM Contact} " & _
      "APPEND ({SELECT ContactID, OrderID FROM Orders} " & _
      "RELATE ContactID TO ContactID) As rsDetails, " & _
      " COUNT(rsDetails.OrderID) AS CountOfOrder"

      Set .ActiveConnection = con
      .Open

      .Fields("ContactName").Value = "Pink Cat"

      .UpdateBatch

      MsgBox .GetString
      .Close
    End With

    With rs
      .Source = _
      "SELECT ContactID, ContactName FROM Contact"

      Set .ActiveConnection = con
      .Open

      MsgBox .GetString
      .Close

    End With
  End With
End Sub

When using the MSDataShape OLE DE provider and the OLE DB provider for Jet (or ACE) then it is indeed possible to create an updateable ADO recordset and APPEND a computed column based on a set function such as COUNT(). The resulting SQL-esque code would look more like this:

 SHAPE  {SELECT ContactID, ContactName FROM Contact} 
APPEND ({SELECT ContactID, OrderID FROM Orders} 
         RELATE ContactID TO ContactID
       ) As rsDetails, COUNT(rsDetails.OrderID) AS CountOfOrder

Here's a brief 'proof of concept': paste the following into any VBA module (e.g. use Excel), no references required, creates a new .mdb in your temp directory, creates the tables with data, to prove the recordset is updateable the ContactName value is changed and the recordset reopened to show it has indeed changed:

Sub ShapeAppendCount()

  On Error Resume Next
  Kill Environ$("temp") & "\DropMe.mdb"
  On Error GoTo 0

  Dim cat
  Set cat = CreateObject("ADOX.Catalog")
  With cat
    .Create _
        "Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source=" & _
        Environ$("temp") & "\DropMe.mdb"

    Dim jeng
    Set jeng = CreateObject("JRO.JetEngine")
    jeng.RefreshCache .ActiveConnection

    Set .ActiveConnection = Nothing
  End With

  Dim con
  Set con = CreateObject("ADODB.Connection")
  With con
    .ConnectionString = _
        "Provider=MSDataShape;" & _
        "Data Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source=" & _
        Environ$("temp") & "\DropMe.mdb"
    .CursorLocation = 3
    .Open

    .Execute _
    "CREATE TABLE Contact (" & _
    "ContactID INTEGER NOT NULL UNIQUE, " & _
    "ContactName VARCHAR(20) NOT NULL);"

    .Execute _
    "CREATE TABLE Orders (" & _
    "ContactID INTEGER NOT NULL REFERENCES Contact (ContactID), " & _
    "OrderID INTEGER NOT NULL UNIQUE);"

    .Execute _
    "INSERT INTO Contact (ContactID, ContactName)" & _
    " VALUES (1, 'OneDayWhen');"

    .Execute _
    "INSERT INTO Orders (ContactID, OrderID)" & _
    " VALUES (1, 1);"

    .Execute _
    "INSERT INTO Orders (ContactID, OrderID)" & _
    " VALUES (1, 2);"

    Dim rs
    Set rs = CreateObject("ADODB.Recordset")
    With rs
      .CursorType = 2  ' adOpenDynamic
      .LockType = 4  ' adLockBatchOptimistic

      .Source = _
      " SHAPE {SELECT ContactID, ContactName FROM Contact} " & _
      "APPEND ({SELECT ContactID, OrderID FROM Orders} " & _
      "RELATE ContactID TO ContactID) As rsDetails, " & _
      " COUNT(rsDetails.OrderID) AS CountOfOrder"

      Set .ActiveConnection = con
      .Open

      .Fields("ContactName").Value = "Pink Cat"

      .UpdateBatch

      MsgBox .GetString
      .Close
    End With

    With rs
      .Source = _
      "SELECT ContactID, ContactName FROM Contact"

      Set .ActiveConnection = con
      .Open

      MsgBox .GetString
      .Close

    End With
  End With
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文