寫在前面
你是否在實際開發程序中遇到過這樣的苦惱,接手一個需求后,設計了很多張表,沒有采用power designer設計表,而是參考歷史業務在檔案中羅列的表欄位,表很多且欄位也很多,多到你復制粘貼都覺得慢,這時候作為一個愛思(tou)考(lan)的你,肯定想能夠快速生成創建表陳述句,那么解決方案他來了,
正文
1、軟體安裝
首先,我們還是需要本地安裝Power desinger這個工具的,其次你本地需要有安裝excel,
(關于Power Designer的下載,請點擊此處,提取碼m6z5,如果覺得太慢,可以關注我微信公眾號:半路猿,加我好友,我直接發給你哈,至于安裝,相信難不倒你,excel軟體,就不用我發了吧!)
2、資料處理
將你設計的表欄位復制到excel中,如果有多個表,則分sheet分別復制進去,如下圖所示,

上圖格式說明:
1. 第一行第一列填寫表描述,第一行第二列填寫表名
2. 從第二行開始,第一列填寫欄位名,第二列填寫欄位型別,第三列填寫欄位描述,第四列填寫是否必填(其中否代表不能為空,是代表可以為空)
3、利用excel生成Power Designer表模型
多圖警告!!!



彈出以下視窗,如圖:

將以下內容復制到編輯框中,點擊Run便可以創建出我們的表模型啦,
'開始
Option Explicit
Dim mdl ' the current model
Set mdl = ActiveModel
If (mdl Is Nothing) Then
MsgBox "There is no Active Model"
End If
Dim HaveExcel
Dim RQ
RQ = vbYes 'MsgBox("Is Excel Installed on your machine ?", vbYesNo + vbInformation, "Confirmation")
If RQ = vbYes Then
HaveExcel = True
' Open & Create Excel Document
Dim x1 '
Set x1 = CreateObject("Excel.Application")
x1.Workbooks.Open "E:\sql\sql.xlsx" '指定 excel檔案路徑(記得換成你自己的檔案路徑)
Else
HaveExcel = False
End If
a x1, mdl
sub a(x1, mdl)
dim rwIndex
dim tableName
dim colname
dim table
dim col
dim count
dim sheetIndex
on error Resume Next
For sheetIndex = 1 To 200 '指定要遍歷的 Sheet
x1.Workbooks(1).Worksheets(sheetIndex).Activate '指定要打開的sheet名稱
With x1.Workbooks(1).Worksheets(sheetIndex)
If .Cells(1, 1).Value = "" Then
Exit For
End If
set table = mdl.Tables.CreateNew '創建一個 表物體
table.Name = .Cells(1, 1).Value '指定 表名
table.Code = .Cells(1, 2).Value '指定 表名
count = count + 1
For rwIndex = 2 To 1000 '指定要遍歷的 Excel行標, 從第2行開始
If .Cells(rwIndex, 1).Value = "" Then
Exit For
End If
set col = table.Columns.CreateNew '創建一列/欄位
'MsgBox .Cells(rwIndex, 1).Value, vbOK + vbInformation, "列"
If .Cells(rwIndex, 1).Value = "" Then
col.Name = .Cells(rwIndex, 2).Value '指定列名-----如果第1列(Name)為空,則顯示第2列的Code
Else
col.Name = .Cells(rwIndex, 1).Value
End If
'MsgBox col.Name, vbOK + vbInformation, "列"
col.Code = .Cells(rwIndex, 1).Value '指定列名-------第1列是欄位名
col.DataType = .Cells(rwIndex, 2).Value '指定列資料型別-----第2列是型別
col.Comment = .Cells(rwIndex, 3).Value '指定列說明-------第3列是列說明
If .Cells(rwIndex, 4).Value = "否" Then
col.Mandatory = true '指定列是否可空 true 為不可空 ------第4列指定列是否允許為空
End If
If rwIndex = 2 Then
col.Primary = true '指定主鍵-------第2行是主鍵列
End If
Next
End With
Next
MsgBox "生成資料 表結構共計 " + CStr(count),vbInformation, " 表"
Exit Sub
End sub
成功結果如圖:

最后,就可以看到我們的創建表陳述句啦

4、復制創建表陳述句,利用IDEA批處理進行完善
到這里,我們可能發現創建的表沒有表名注釋或者是指定masql的存盤引擎,或者是沒有欄位的默認值等情況,我們可以隨便打開一個編輯器,對其進行修飾就可以啦~~
寫在最后
看到這里,是不是覺得自己開發效率又能提高一大截啦,如果覺得對你有幫助,可以點贊收藏加關注,或者是關注我的微信公眾號:半路猿,一起學習,一同進步,
參考文章:
https://www.cnblogs.com/roboot/p/9956071.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/172907.html
標籤:其他
下一篇:安全管理——訪問控制&用戶管理
