数据字典到SQL语句的转换(使用word与VBA)

一般我们有几种方式对数据库建模:

1.采用PowerDesign或ErWin等建模工具进行建模

2.利用WORD或EXCEL进行建模

使用第一种方式,可以自动导出要生产表的SQL语句,建表比较方便,而对于第二种方式,则可能需要手工建表,其实在WORD中可以用VBA完成对SQL语句的生成,如将下面格式的WORD数据字典生成SQL语句,就可以利用VBA进行

 

WORD文档样式为:

 

Table1

 

 

 

 

 

 

PK

 默认值

Field1

 

Varchar(50)

 1

Field2

 

Int

 

 

Field3

 

Int

 

 

Field4

 

Numeric(18,4)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Function GetCreateStr()
Dim docOld As Document
Dim rngDoc As Range
Dim tblDoc As Table
Dim i As Integer, iIndex1 As Integer, iIndex2 As Integer
Dim myCell As Cell
Dim sTemp As String, sTemp1 As String, sTemp2 As String
Dim sHead As String
Dim sResult As String, sLength As String
Dim sDou As String, s1 As String
Dim sDou1 As String, sKey As String
Dim sTableName As String

sDou = """": sDou1 = "'"
If ActiveDocument.Tables.Count >= 1 Then
For i = 1 To ActiveDocument.Tables.Count
sTableName = ActiveDocument.Tables(i).Cell(1, 2).Range.Text
sTableName = Mid(sTableName, 1, Len(sTableName) - 2)
s1 = "USE [SocialKey]" & vbCrLf & vbCrLf & "GO " & vbCrLf & vbCrLf & _
"/****** 对象: Table [dbo].[" & sTableName & "] 脚本日期: " & Now & _
"******/" & vbCrLf & vbCrLf & _
"SET ANSI_NULLS ON " & vbCrLf & vbCrLf & _
"SET QUOTED_IDENTIFIER ON " & vbCrLf & _
"GO " & vbCrLf

sHead = s1 & vbctrl & "if exists (select * from dbo.sysobjects where id = object_id(N" & sDou1 & _
"[dbo].[" & sTableName & "]" & sDou1 & ") and OBJECTPROPERTY(id, N" & sDou1 & _
"IsUserTable" & sDou1 & ") = 1)" & vbCrLf & _
vbTab & " drop table [dbo].[" & sTableName & "]" & vbCrLf & _
"GO" & vbCrLf & _
"CREATE TABLE [dbo].[" & sTableName & "] ("

sTemp = "": sResult = ""

For j = 3 To ActiveDocument.Tables(i).Rows.Count
Set myCell = ActiveDocument.Tables(i).Cell(j, 1)
sTemp = Mid(myCell.Range.Text, 1, Len(myCell.Range.Text) - 2)
If Trim(sTemp) <> "" Then
sTemp = vbTab & "[" & sTemp & "]"

Set myCell = ActiveDocument.Tables(i).Cell(j, 3)
sTemp1 = Mid(myCell.Range.Text, 1, Len(myCell.Range.Text) - 2)
iIndex1 = InStr(sTemp1, "(")
If iIndex1 > 0 Then
iIndex2 = InStr(sTemp1, ")")
sLength = Mid(sTemp1, iIndex1 + 1, iIndex2 - iIndex1 - 1)
sTemp1 = Mid(sTemp1, 1, iIndex1 - 1)
sTemp2 = "[" & sTemp1 & "]" & " (" & sLength & ")" & " NULL" & ","

Else
sTemp2 = "[" & sTemp1 & "]" & " NULL" & ","
End If
sResult = sResult & sTemp & " " & sTemp2 & vbCrLf
End If
Next j
sResult = sHead & vbCrLf & sResult
Next i
End If
sResult = Mid(sResult, 1, Len(sResult) - 3) & vbCrLf & ")"
GetCreateStr = sResult
'Debug.Print GetCreateStr
End Function


Sub StrToFile(sContent As String, sFile As String)
Kill sFile '首先删除源文件
Open sFile For Append As #1 '打开文件
Print #1, sContent '写入文件
Close #1
End Sub



Private Sub CommandButton2_Click()
Dim sTemp As String, sPath As String
sTemp = GetCreateStr
'Debug.Print sTemp
sPath = Left(ActiveDocument.Name, Len(ActiveDocument.Name) - 4)
'Debug.Print ActiveDocument.Path & sPath & ".txt"
Call StrToFile(sTemp, ActiveDocument.Path & "\" & sPath & ".txt")
End Sub

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值