Access: create table if it does not exist

前端 未结 3 2141
南笙
南笙 2020-12-11 22:52

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

Update

Something like this

IF 

        
相关标签:
3条回答
  • 2020-12-11 23:05

    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

    0 讨论(0)
  • 2020-12-11 23:14

    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 :(

    0 讨论(0)
  • 2020-12-11 23:32

    Here is how to do it via 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
    
    0 讨论(0)
提交回复
热议问题