代码之家  ›  专栏  ›  技术社区  ›  Richard A

从Microsoft Access创建表DDL

  •  14
  • Richard A  · 技术社区  · 17 年前

    我有大约30个表,我们正在将它们移植到Oracle,如果我们能够从Access定义创建这些表,那么我们的工作就会更轻松。

    6 回复  |  直到 17 年前
        1
  •  23
  •   Richard A    17 年前

    谢谢你的其他建议。在我等待的时候,我编写了一些VBA代码来实现它。它并不完美,但为我完成了任务。

    Option Compare Database
    Public Function TableCreateDDL(TableDef As TableDef) As String
    
             Dim fldDef As Field
             Dim FieldIndex As Integer
             Dim fldName As String, fldDataInfo As String
             Dim DDL As String
             Dim TableName As String
    
             TableName = TableDef.Name
             TableName = Replace(TableName, " ", "_")
             DDL = "create table " & TableName & "(" & vbCrLf
             With TableDef
                For FieldIndex = 0 To .Fields.Count - 1
                   Set fldDef = .Fields(FieldIndex)
                   With fldDef
                      fldName = .Name
                      fldName = Replace(fldName, " ", "_")
                      Select Case .Type
                         Case dbBoolean
                            fldDataInfo = "nvarchar2"
                         Case dbByte
                            fldDataInfo = "number"
                         Case dbInteger
                            fldDataInfo = "number"
                         Case dbLong
                            fldDataInfo = "number"
                         Case dbCurrency
                            fldDataInfo = "number"
                         Case dbSingle
                            fldDataInfo = "number"
                         Case dbDouble
                            fldDataInfo = "number"
                         Case dbDate
                            fldDataInfo = "date"
                         Case dbText
                            fldDataInfo = "nvarchar2(" & Format$(.Size) & ")"
                         Case dbLongBinary
                            fldDataInfo = "****"
                         Case dbMemo
                            fldDataInfo = "****"
                         Case dbGUID
                            fldDataInfo = "nvarchar2(16)"
                      End Select
                   End With
                   If FieldIndex > 0 Then
                   DDL = DDL & ", " & vbCrLf
                   End If
                   DDL = DDL & "  " & fldName & " " & fldDataInfo
                   Next FieldIndex
             End With
             DDL = DDL & ");"
             TableCreateDDL = DDL
    End Function
    
    
    Sub ExportAllTableCreateDDL()
    
        Dim lTbl As Long
        Dim dBase As Database
        Dim Handle As Integer
    
        Set dBase = CurrentDb
    
        Handle = FreeFile
    
        Open "c:\export\TableCreateDDL.txt" For Output Access Write As #Handle
    
        For lTbl = 0 To dBase.TableDefs.Count - 1
             'If the table name is a temporary or system table then ignore it
            If Left(dBase.TableDefs(lTbl).Name, 1) = "~" Or _
            Left(dBase.TableDefs(lTbl).Name, 4) = "MSYS" Then
                 '~ indicates a temporary table
                 'MSYS indicates a system level table
            Else
              Print #Handle, TableCreateDDL(dBase.TableDefs(lTbl))
            End If
        Next lTbl
        Close Handle
        Set dBase = Nothing
    End Sub
    

        2
  •  4
  •   Corey Trager    17 年前

    我已经做到了:

    有一个工具可以从访问SQL Server“升迁”。这样做,然后使用优秀的SQL Server工具生成脚本。

    http://support.microsoft.com/kb/237980

        3
  •  2
  •   Michael OShea    17 年前

    有一个关于将Access数据库转换为Oracle的完整教程 here . 如果这只是你想要的结构,那么你可以专注于第3.0节。

        4
  •  1
  •   Gareth    17 年前

    可以使用Access中的导出功能将表导出到ODBC数据源。将ODBC数据源设置为Oracle数据库,然后右键单击Access“Tables”选项卡中的表并选择导出。ODBC是“文件格式”之一,它将打开通常的ODBC对话框。

        5
  •  1
  •   skamradt    17 年前

    您可能需要查看ADOX以获取模式信息。使用ADOX,您可以获得诸如键、视图、关系等内容。

    不幸的是,我不是一个VB程序员,但是网上有很多使用ADOX获取表格模式的例子。

        6
  •  0
  •   Stareagle    9 年前

    派对有点晚了,但我使用RazorSQL为Access数据库生成DDL。