我是不是再次破坏了 Access 数据库引擎?

Posted

技术标签:

【中文标题】我是不是再次破坏了 Access 数据库引擎?【英文标题】:Have I broken the Access Database Engine again?我是否再次破坏了 Access 数据库引擎? 【发布时间】:2009-10-21 08:16:01 【问题描述】:

在为another *** answer 测试以下 SQL 代码时,我收到以下错误:

mdb 通过 OLE DB:“灾难性 失败” accdb 通过 OLE DB:(空白消息) Access2007 查询对象:“未知 访问数据库错误”

我正在使用一个新的干净的数据库文件。 showplan.out 执行计划不包含任何有用的东西;我已经发布了以下内容。

除了声明 Access 数据库引擎不适合用途 (!!),我还能做些什么吗?

Sub Discon()

  Const USE_MDB As Long = 1
  Const USE_ACCDB As Long = 2

  Dim version As Long

  ' Hard code which version of the
  ' Access Database Engine to use

  version = USE_MDB

  Dim fullFileName As String
  Dim conString As String

  If version = USE_MDB Then

    fullFileName = Environ$("temp") & "\DropMe.mdb"
    conString = _
        "Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source=" & fullFileName

  Else

    fullFileName = Environ$("temp") & "\DropMe.accdb"
    conString = _
        "Provider=Microsoft.ACE.OLEDB.12.0;" & _
        "Data Source=" & fullFileName

  End If

  On Error Resume Next
  Kill fullFileName
  On Error GoTo 0

  Dim cat
  Set cat = CreateObject("ADOX.Catalog")
  With cat

    ' Create a new database file in user's temp folder
    .Create conString

    With .ActiveConnection

      Dim Sql As String

      ' Create a new base base with data (required to
      ' be able to later create a virtual table)

      Sql = _
      "CREATE TABLE Customers (" & _
      "CustomerID CHAR(5) NOT NULL UNIQUE);"
      .Execute Sql

      Sql = _
      "INSERT INTO Customers (CustomerID)" & _
      " VALUES ('ANTON');"
      .Execute Sql

      ' Create a virtual (viewed) table
      Sql = _
      "CREATE VIEW TableA AS  " & _
      "SELECT DT1.ID, DT1.[Date], DT1.Supplier_ID " & _
      "FROM ( " & _
      "      SELECT DISTINCT 1 AS ID, '2009-10-23 00:00:00' AS [Date], " & _
      "             1 AS Supplier_ID FROM Customers " & _
      "      UNION ALL  " & _
      "      SELECT DISTINCT 2, '2009-10-23 00:00:00', 1 FROM Customers" & _
      "      UNION ALL  " & _
      "      SELECT DISTINCT 3, '2009-10-24 00:00:00', 2 FROM Customers " & _
      "      UNION ALL  " & _
      "      SELECT DISTINCT 4, '2009-10-25 00:00:00', 2 FROM Customers " & _
      "      UNION ALL  " & _
      "      SELECT DISTINCT 5, '2009-10-26 00:00:00', 1  FROM Customers " & _
      "     ) AS DT1;"
      .Execute Sql

      ' Create VIEWs based on the virtual table

      Sql = _
      "CREATE VIEW TableA_StartDates (Supplier_ID, start_date) " & _
      "AS " & _
      "SELECT T1.Supplier_ID, T1.[Date] " & _
      "  FROM TableA AS T1 " & _
      " WHERE NOT EXISTS ( " & _
      "                   SELECT * " & _
      "                     FROM TableA AS T2 " & _
      "                    WHERE T2.Supplier_ID = T1.Supplier_ID " & _
      "                          AND DATEADD('D', -1, T1.[Date]) = T2.[Date] " & _
      "                  );"
      .Execute Sql

      Sql = _
      "CREATE VIEW TableA_EndDates (Supplier_ID, end_date) " & _
      "AS " & _
      "SELECT T3.Supplier_ID, T3.[Date] " & _
      "  FROM TableA AS T3 " & _
      " WHERE NOT EXISTS ( " & _
      "                   SELECT * " & _
      "                     FROM TableA AS T4 " & _
      "                    WHERE T4.Supplier_ID = T3.Supplier_ID " & _
      "                          AND DATEADD('D', 1, T3.[Date]) = T4.[Date] " & _
      "                  );"
      .Execute Sql

      Sql = _
      "CREATE VIEW TableA_Periods (Supplier_ID, start_date, end_date) " & _
      "AS " & _
      "SELECT DISTINCT T5.Supplier_ID, " & _
      "       ( " & _
      "        SELECT MAX(S1.start_date) " & _
      "          FROM TableA_StartDates AS S1 " & _
      "         WHERE S1.Supplier_ID = T5.Supplier_ID " & _
      "               AND S1.start_date <= T5.[Date] " & _
      "       ), " & _
      "       ( " & _
      "        SELECT MIN(E1.end_date) " & _
      "          FROM TableA_EndDates AS E1 " & _
      "         WHERE E1.Supplier_ID = T5.Supplier_ID " & _
      "               AND T5.[Date] <= E1.end_date " & _
      "       )         " & _
      "  FROM TableA AS T5;"
      .Execute Sql

      ' Attempt to use the nested VIEWs in a query

      Sql = _
      "SELECT * FROM TableA_Periods AS P1;"

      Dim rs

      On Error Resume Next
      Set rs = .Execute(Sql)

      If Err.Number = 0 Then
        MsgBox rs.GetString
      Else
        MsgBox _
            Err.Number & ": " & _
            Err.Description & _
            " (" & Err.Source & ")"
      End If

      On Error GoTo 0

    End With
    Set .ActiveConnection = Nothing
  End With
End Sub

这是showplan.out的内容:

--- temp query ---

- Inputs to Query -
- End inputs to Query -

01) Insert into 'Customers'



--- temp query ---

- Inputs to Query -
- End inputs to Query -

01) Insert into 'Customers'



--- temp query ---

- Inputs to Query -
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
- End inputs to Query -

      store result in temporary table
      store result in temporary table
01) Union result of '00)' and result of '00)'
      store result in temporary table
02) Union result of '01)' and result of '01)'
      store result in temporary table
03) Union result of '02)' and result of '02)'
      store result in temporary table
04) Union result of '03)' and result of '03)'



--- temp query ---

- Inputs to Query -
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
- End inputs to Query -

      store result in temporary table
      store result in temporary table
01) Union result of '00)' and result of '00)'
      store result in temporary table
02) Union result of '01)' and result of '01)'
      store result in temporary table
03) Union result of '02)' and result of '02)'
      store result in temporary table
04) Union result of '03)' and result of '03)'
05) Restrict rows of result of 04)
      by scanning
      testing expression "Not "



--- temp query ---

- Inputs to Query -
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
- End inputs to Query -

      store result in temporary table
      store result in temporary table
01) Union result of '00)' and result of '00)'
      store result in temporary table
02) Union result of '01)' and result of '01)'
      store result in temporary table
03) Union result of '02)' and result of '02)'
      store result in temporary table
04) Union result of '03)' and result of '03)'
05) Restrict rows of result of 04)
      by scanning
      testing expression "Not "



--- temp query ---

- Inputs to Query -
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
- End inputs to Query -

      store result in temporary table
      store result in temporary table
01) Union result of '00)' and result of '00)'
      store result in temporary table
02) Union result of '01)' and result of '01)'
      store result in temporary table
03) Union result of '02)' and result of '02)'
      store result in temporary table
04) Union result of '03)' and result of '03)'
05) Restrict rows of result of 04)
      by scanning
      testing expression "Not "



--- temp query ---

- Inputs to Query -
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
- End inputs to Query -

      store result in temporary table
      store result in temporary table
01) Union result of '00)' and result of '00)'
      store result in temporary table
02) Union result of '01)' and result of '01)'
      store result in temporary table
03) Union result of '02)' and result of '02)'
      store result in temporary table
04) Union result of '03)' and result of '03)'
05) Restrict rows of result of 04)
      by scanning
      testing expression "Not "



--- temp query ---

- Inputs to Query -
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
- End inputs to Query -

      store result in temporary table
      store result in temporary table
01) Union result of '00)' and result of '00)'
      store result in temporary table
02) Union result of '01)' and result of '01)'
      store result in temporary table
03) Union result of '02)' and result of '02)'
      store result in temporary table
04) Union result of '03)' and result of '03)'
      store result in temporary table



--- temp query ---

- Inputs to Query -
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
- End inputs to Query -

      store result in temporary table
      store result in temporary table
01) Union result of '00)' and result of '00)'
      store result in temporary table
02) Union result of '01)' and result of '01)'
      store result in temporary table
03) Union result of '02)' and result of '02)'
      store result in temporary table
04) Union result of '03)' and result of '03)'
      store result in temporary table



--- temp query ---

- Inputs to Query -
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
- End inputs to Query -

      store result in temporary table
      store result in temporary table
01) Union result of '00)' and result of '00)'
      store result in temporary table
02) Union result of '01)' and result of '01)'
      store result in temporary table
03) Union result of '02)' and result of '02)'
      store result in temporary table
04) Union result of '03)' and result of '03)'
      store result in temporary table



--- temp query ---

- Inputs to Query -
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
Table 'Customers'
- End inputs to Query -

      store result in temporary table
      store result in temporary table
01) Union result of '00)' and result of '00)'
      store result in temporary table
02) Union result of '01)' and result of '01)'
      store result in temporary table
03) Union result of '02)' and result of '02)'
      store result in temporary table
04) Union result of '03)' and result of '03)'
      store result in temporary table

【问题讨论】:

+1 为您的问题标题,因为它让我发笑,但很遗憾,我无能为力,因为多年前我很遗憾地离开了 Access。 这是 Jet 4.0 的东西还是您可以在早期版本中重现它? @heferav:“你能在早期版本中重现它吗?” -- 不可能:早期版本不支持 CREATE VIEW 也不支持 EXISTS。 “这是 Jet 4.0 的东西吗”——我可以在更高版本的 ACE(Access2007 的引擎版本)中重现它。 你想做什么。我当然不明白你想在这里测试什么。使用 DAO 从 Access 创建 Jet/ACE 数据存储对我来说似乎是不自然的。当您的开发环境不是 Access 时,等式翻转到 ADO,但我只是看不到在此处使用非本机数据接口层的理由。 @David W. Fenton:“你想做什么”——见 HansUp 的回答。他的分析是即时的,即即时“物化”一张桌子。比我解释得更好:) 【参考方案1】:

声明 Access 数据库引擎不适合用途 (!!)。这些是普通查询,应该可以正常工作。

【讨论】:

完全同意。我讨厌 MS Access / Jet.. 我喜欢 Access Data Projects——这是一种在 SQL Server 和 MS Access 之间进行通信的更有效的方式。我对 MS Access 2013 的下一代真正的客户端-服务器软件感到兴奋。 @AaronKempf:Aaron,很高兴再次收到您的来信。我们多年来一直在交换镜头,这可能是我们达成一致意见的少数场合之一。有关记忆之旅,请参阅:mofeel.net/1-microsoft-public-access-adp-sqlserver/2808-3.aspx yah.. 我现在试着变得更有说服力:)

以上是关于我是不是再次破坏了 Access 数据库引擎?的主要内容,如果未能解决你的问题,请参考以下文章

access 文件被破坏了,打不开。

access数据库被破坏,如何修复?

Access 2003 SQL Switch 会破坏数据类型?

Access 数据库引擎和 Microsoft Access 之间的不兼容问题

宏 - 查询 - Microsoft Access 数据库引擎停止了进程

导出有错误的 Access 数据库