SQL 检查访问表是否存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2985513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 06:28:12  来源:igfitidea点击:

Check if access table exists

sqlms-accessms-access-2007

提问by HasanG

I want to log web site visits' IP, datetime, client and refferer data to access database but I'm planning to log every days log data in separate tables in example logs for 06.06.2010 will be logged in 2010_06_06 named table. When date is changed I'll create a table named 2010_06_07. But the problem is if this table is already created.

我想记录网站访问的 IP、日期时间、客户端和引用者数据以访问数据库,但我计划每天将日志数据记录在单独的表中,例如 06.06.2010 的示例日志将记录在 2010_06_06 命名表中。更改日期后,我将创建一个名为 2010_06_07 的表。但问题是这个表是否已经创建。

Any suggestions how to check if table exists in Access?

任何关于如何检查 Access 中是否存在表的建议?

回答by Fionnuala

You can use the hidden system table MSysObjects to check if a table exists:

您可以使用隐藏的系统表 MSysObjects 来检查表是否存在:

If Not IsNull(DlookUp("Name","MSysObjects","Name='TableName'")) Then
    'Table Exists

However, I agree that it is a very bad idea to create a new table every day.

但是,我同意每天创建一个新表是一个非常糟糕的主意。

EDIT: I should add that tables have a type 1, 4 or 6 and it is possible for other objects of a different type to have the same name as a table, so it would be better to say:

编辑:我应该补充一点,表的类型为 1、4 或 6,并且不同类型的其他对象可能与表具有相同的名称,因此最好说:

If Not IsNull(DlookUp("Name","MSysObjects","Name='TableName' And Type In (1,4,6)")) Then
    'Table Exists

However, it is not possible to create a table with the same name as a query, so if you need a look up to test for a name, it may be best to add 5, that is query, to the Type list.

但是,无法创建与查询同名的表,因此如果您需要查找来测试名称,最好在类型列表中添加 5,即查询。

回答by KevenDenen

Here's another solution, will be a bit faster than looping over all of the tables.

这是另一种解决方案,比循环遍历所有表要快一些。

Public Function doesTableExist(strTableName As String) As Boolean
    Dim db As DAO.Database
    Dim td As DAO.TableDef
    Set db = CurrentDb
    On Error Resume Next
    Set td = db.TableDefs(strTableName)
    doesTableExist = (Err.Number = 0)
    Err.Clear
End Function

回答by David-W-Fenton

I tested various methods for finding out if a table exists several years ago. Here is the code for all of them as I implemented, including my simple test routine.

几年前,我测试了各种方法来确定表是否存在。这是我实现的所有代码,包括我的简单测试例程。

Public Function TableExists(strTableName As String, Optional ysnRefresh As Boolean, Optional db As DAO.Database) As Boolean
' Originally Based on Tony Toews function in TempTables.MDB, http://www.granite.ab.ca/access/temptables.htm
' Based on testing, when passed an existing database variable, this is the fastest
On Error GoTo errHandler
  Dim tdf As DAO.TableDef

  If db Is Nothing Then Set db = CurrentDb()
  If ysnRefresh Then db.TableDefs.Refresh
  Set tdf = db(strTableName)
  TableExists = True

exitRoutine:
  Set tdf = Nothing
  Exit Function

errHandler:
  Select Case Err.Number
    Case 3265
      TableExists = False
    Case Else
      MsgBox Err.Number & ": " & Err.Description, vbCritical, "Error in mdlBackup.TableExists()"
  End Select
  Resume exitRoutine
End Function

Public Function TableExists2(strTableName As String, Optional ysnRefresh As Boolean, Optional db As DAO.Database) As Boolean
On Error GoTo errHandler
  Dim bolCleanupDB As Boolean
  Dim tdf As DAO.TableDef

  If db Is Nothing Then
     Set db = CurrentDb()
     bolCleanupDB = True
  End If
  If ysnRefresh Then db.TableDefs.Refresh
  For Each tdf In db.TableDefs
    If tdf.name = strTableName Then
       TableExists2 = True
       Exit For
    End If
  Next tdf

exitRoutine:
  Set tdf = Nothing
  If bolCleanupDB Then
     Set db = Nothing
  End If
  Exit Function

errHandler:
  MsgBox Err.Number & ": " & Err.Description, vbCritical, "Error in mdlBackup.TableExists1()"
  Resume exitRoutine
End Function

Public Function TableExists3(strTableName As String, _
     Optional db As DAO.Database) As Boolean
' Based on testing, when NOT passed an existing database variable, this is the fastest
On Error GoTo errHandler
  Dim strSQL As String
  Dim rs As DAO.Recordset

  If db Is Nothing Then Set db = CurrentDb()
  strSQL = "SELECT MSysObjects.Name FROM MSysObjects "
  strSQL = strSQL & "WHERE MSysObjects.Name=" & Chr(34) & strTableName & Chr(34)
  strSQL = strSQL & " AND MSysObjects.Type=6;"
  Set rs = db.OpenRecordset(strSQL)
  TableExists3 = (rs.RecordCount <> 0)

exitRoutine:
  If Not (rs Is Nothing) Then
     rs.Close
     Set rs = Nothing
  End If
  Exit Function

errHandler:
  MsgBox Err.Number & ": " & Err.Description, vbCritical, _
     "Error in TableExists1()"
  Resume exitRoutine
End Function

Public Sub TestTableExists(strTableName As String, intLoopCount As Integer)
  Dim dteStart As Date
  Dim i As Integer
  Dim bolResults As Boolean

  dteStart = Now()
  For i = 0 To intLoopCount
    bolResults = TableExists(strTableName, , CurrentDB())
  Next i
  Debug.Print "TableExists (" & intLoopCount & "): " & Format(Now() - dteStart, "nn:ss")

  dteStart = Now()
  For i = 0 To intLoopCount
    bolResults = TableExists2(strTableName, , CurrentDB())
  Next i
  Debug.Print "TableExists2 (" & intLoopCount & "): " & Format(Now() - dteStart, "nn:ss")

  dteStart = Now()
  For i = 0 To intLoopCount
    bolResults = TableExists3(strTableName, CurrentDB())
  Next i
  Debug.Print "TableExists3 (" & intLoopCount & "): " & Format(Now() - dteStart, "nn:ss")
End Sub

回答by Leo Orientis

I have found querying system tables or tabledefs to be unreliable and introduce unpredictable behaviour in scripts where tables get regularly created and dropped.

我发现查询系统表或 tabledef 是不可靠的,并且会在定期创建和删除表的脚本中引入不可预测的行为。

Based on my results, my hypothesis is that these tables aren't necessarily updated at the exact instant a CREATEor DROPis executed, or that concurrency issues are preventing me from getting an accurate result.

根据我的结果,我的假设是这些表不一定在 aCREATEDROP执行的确切时刻更新,或者并发问题使我无法获得准确的结果。

I've found the following method to be more reliable:

我发现以下方法更可靠:

Public Function TableExists(theDatabase As Access.Application, _
    tableName As String) As Boolean

    ' Presume that table does not exist.
    TableExists = False

    ' Define iterator to query the object model.
    Dim iTable As Integer

    ' Loop through object catalogue and compare with search term.
    For iTable = 0 To theDatabase.CurrentData.AllTables.Count - 1
        If theDatabase.CurrentData.AllTables(iTable).Name = tableName Then
            TableExists = True
            Exit Function
        End If
    Next iTable

End Function

There should be no runtime issue iterating unless there is an staggeringly enormous collection of tables.

除非有非常庞大的表集合,否则不应该有运行时问题迭代。

回答by Patrick Honorez

This question is quite old but I found that no answer is satisfying, because:

这个问题很老了,但我发现没有令人满意的答案,因为:

  • they do not handle the case of "bad" linked tables, where the linked table points to a non existing db or table.
  • since linked tables are potentially huge, we must be able to check them with a fast query.
  • 它们不处理“坏”链接表的情况,其中链接表指向不存在的数据库或表。
  • 由于链接表可能很大,我们必须能够通过快速查询来检查它们。

So here is my simple but more complete solution:

所以这是我的简单但更完整的解决方案:

Function isTableOk(tblName As String) As Boolean
'works with local or linked tables
    Dim db As DAO.Database, rs As DAO.Recordset
    Dim sSql As String
    sSql = "SELECT TOP 1 ""xxx"" AS Expr1 FROM [" & tblName & "]"

    On Error Resume Next
    Err.Clear
    Set db = CurrentDb
    Set rs = db.OpenRecordset(sSql)
    isTableOk = (Err.Number = 0)
    rs.Close
End Function

You can even check table in an externalAccess db with this version:

您甚至可以使用此版本检查外部Access 数据库中的表:

Function isTableOk(tblName As String, Optional dbName As String) As Boolean
'works with local or linked tables, or even tables in external db (if dbName is provided)

    Dim db As DAO.Database, rs As DAO.Recordset
    Dim sSql As String

    Set db = CurrentDb
    sSql = "SELECT TOP 1 'xxx' AS Expr1 FROM [" & tblName & "]"
    If Len(dbName) > 0 Then 'external db 
        sSql = sSql & " IN '" & dbName & "'"
    End If
    Err.Clear
    On Error Resume Next
    Set rs = db.OpenRecordset(sSql)
    isTableOk = (Err.Number = 0)
    rs.Close
End Function