VBA从Excel中生成Oracle create table

来源:互联网 发布:淘宝运营工资大概多少 编辑:程序博客网 时间:2024/06/06 01:43
有时会碰到Oracle设计文档用Excel,需要根据Excel生成create table语句。当表多,列多,设计变更时,这个VBA小函数就能节省很多时间。在google, baidu搜了一圈,没有找到类似的。写出来方便自己也方便他人。

假设:
Sheet名字为表名
第一行作为Excel表格表头
第一列为列名
第二列null 约束
第三列数据类型

目前还不支持分区语法

Private Sub Worksheet_Activate()
    Dim ddl As String
    Dim i As Integer
    ddl = "Create table " + Sheet1.Name + " ("
    'MsgBox (Sheet1.Range("A" & 2).Value)
    For i = 2 To 27
        ddl = ddl + Sheet1.Range("A" & i).Value + " "         'Column name
        ddl = ddl + Sheet1.Range("C" & i).Value + " "              'Data type
        If Sheet1.Range("B" & i).Value = "N" Then
            ddl = ddl + "not null"
        End If
        ddl = ddl + ","
    Next i
    ddl = Left(ddl, Len(ddl) - 1) + ");"
    'Write the DDL to Sheet11
    Sheet11.Range("A2").Value = Sheet1.Name
    Sheet11.Range("B2").Value = ddl
End Sub


0 0