访问:如果表不存在则创建表

时间:2021-03-04 08:42:46

Can you give an MS Access equivalent to MySQL 'CREATE TABLE IF NOT EXISTS ...'?

你能给MS一个相当于MySQL'CREATE TABLE IF NOT NOT EXISTS ......'的MS Access吗?

Update

Something like this

像这样的东西

IF <no such table>
CREATE TABLE history(<fields>)

would be suitable as well

也适合

3 个解决方案

#1


For SQL DDL code the answer is no. ACE/Jet SQL does not have any control-of-flow syntax and a ACE/Jet PROCEDURE can only execute one SQL statement. Yes, that's right: an ACE/Jet PROCEDURE does not support procedural code :(

对于SQL DDL代码,答案是否定的。 ACE / Jet SQL没有任何控制流语法,ACE / Jet PROCEDURE只能执行一个SQL语句。是的,没错:ACE / Jet PROCEDURE不支持程序代码:(

#2


Here is how to do it via VBA:

以下是如何通过VBA执行此操作:

Sub ViaVBA()
    Const strSQLCreateFoo_c As String = _
          "CREATE TABLE Foo" & _
          "(" & _
          "MyField1 INTEGER," & _
          "MyField2 Text(10)" & _
          ");"
    Const strSQLAppendBs_c As String = _
          "INSERT INTO Foo (MyField1, MyField2) " & _
          "SELECT Bar.MyField1, Bar.MyField2 " & _
          "FROM Bar " & _
          "WHERE Bar.MyField2 Like 'B*';"

    If Not TableExists("foo") Then
        CurrentDb.Execute strSQLCreateFoo_c
    End If
    CurrentDb.Execute strSQLAppendBs_c
End Sub

Private Function TableExists(ByVal name As String) As Boolean
    On Error Resume Next
    TableExists = LenB(CurrentDb.TableDefs(name).name)
End Function

#3


Why would you want to create a table? If it's for temporary data storage then that's fine otherwise that's usuaally not required.

你为什么要创建一个表?如果它是用于临时数据存储,那么这很好,否则通常不需要。

See the TempTables.MDB page at my website which illustrates how to use a temporary MDB in your app. http://www.granite.ab.ca/access/temptables.htm

请参阅我的网站上的TempTables.MDB页面,该页面说明了如何在您的应用中使用临时MDB。 http://www.granite.ab.ca/access/temptables.htm

#1


For SQL DDL code the answer is no. ACE/Jet SQL does not have any control-of-flow syntax and a ACE/Jet PROCEDURE can only execute one SQL statement. Yes, that's right: an ACE/Jet PROCEDURE does not support procedural code :(

对于SQL DDL代码,答案是否定的。 ACE / Jet SQL没有任何控制流语法,ACE / Jet PROCEDURE只能执行一个SQL语句。是的,没错:ACE / Jet PROCEDURE不支持程序代码:(

#2


Here is how to do it via VBA:

以下是如何通过VBA执行此操作:

Sub ViaVBA()
    Const strSQLCreateFoo_c As String = _
          "CREATE TABLE Foo" & _
          "(" & _
          "MyField1 INTEGER," & _
          "MyField2 Text(10)" & _
          ");"
    Const strSQLAppendBs_c As String = _
          "INSERT INTO Foo (MyField1, MyField2) " & _
          "SELECT Bar.MyField1, Bar.MyField2 " & _
          "FROM Bar " & _
          "WHERE Bar.MyField2 Like 'B*';"

    If Not TableExists("foo") Then
        CurrentDb.Execute strSQLCreateFoo_c
    End If
    CurrentDb.Execute strSQLAppendBs_c
End Sub

Private Function TableExists(ByVal name As String) As Boolean
    On Error Resume Next
    TableExists = LenB(CurrentDb.TableDefs(name).name)
End Function

#3


Why would you want to create a table? If it's for temporary data storage then that's fine otherwise that's usuaally not required.

你为什么要创建一个表?如果它是用于临时数据存储,那么这很好,否则通常不需要。

See the TempTables.MDB page at my website which illustrates how to use a temporary MDB in your app. http://www.granite.ab.ca/access/temptables.htm

请参阅我的网站上的TempTables.MDB页面,该页面说明了如何在您的应用中使用临时MDB。 http://www.granite.ab.ca/access/temptables.htm