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

时间:2009-05-26 07:26:02

标签: sql ms-access

你能给MS一个等同于MySQL'CREATE TABLE IF NOT NOT EXISTS ......'的文件?

更新

像这样的东西

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

也适合

3 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:4)

以下是如何通过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

答案 2 :(得分:-2)

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

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

相关问题