2008-10-06 155 views
12

是否有任何簡單的方法從Microsoft Access(2007)檢索表創建DDL,還是我必須自己使用VBA編碼來讀取表結構?從Microsoft Access表創建DDL

我有大約30個表,我們正在移植到Oracle,如果我們可以從Access定義中創建表,它會使生活更輕鬆。

回答

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 

我從來沒有聲稱是VB程序員。

+0

不工作的十進制領域 – Peter 2009-10-28 12:10:32

2

我已經做到了這一點:

有從Access到SQL Server的 「升遷」 的工具。做到這一點,然後使用優秀的SQL Server工具來生成腳本。

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

+0

這不相關! – 2009-09-09 11:44:03

+1

@iDevlop - 它*是*相關,但也許我沒有解釋正確。原始問題詢問是否有創建腳本的方法,或者是否必須手動編寫腳本。如果你按照我的指示,你讓MS SQL Server爲你生成腳本。您仍然需要手動調整Access,但SQL Server技巧會爲您帶來90%的優勢。 – 2009-09-10 02:08:36

0

您可以使用導出功能在Access中的表導出到一個ODBC數據源。將ODBC數據源設置爲Oracle數據庫,然後右鍵單擊Access「表」選項卡中的表並選擇導出。 ODBC是「文件格式」之一 - 它將帶來通常的ODBC對話框。

1

您可能想要查看ADOX以獲取架構信息。使用ADOX,您可以獲得諸如按鍵,視圖,關係等的東西。

不幸的是,我不是一個VB程序員,但在Web上使用ADOX獲取表格模式的例子很多。

2

使用Oracle的SQL Developer遷移工作臺。

有關於將Access數據庫轉換爲Oracle的完整教程here。如果只有它的結構,那麼你可以專注於3.0節。

0

晚會有點晚,但我使用RazorSQL爲Access數據庫生成DDL。