博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
数据字典到SQL语句的转换(使用word与VBA)
阅读量:4540 次
发布时间:2019-06-08

本文共 3239 字,大约阅读时间需要 10 分钟。

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

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

 

转载于:https://www.cnblogs.com/djcsch2001/archive/2011/12/16/2290704.html

你可能感兴趣的文章
python全栈脱产第11天------装饰器
查看>>
koa2 从入门到进阶之路 (一)
查看>>
Java / Android 基于Http的多线程下载的实现
查看>>
求职历程-----我的简历
查看>>
[总结]数据结构(板子)
查看>>
网页图片加载失败,用默认图片替换
查看>>
C# 笔记
查看>>
2013年10月13日学习:SQL通过命令语句来创建表
查看>>
剑指offer : 二维数组中的查找
查看>>
第三章 python基础
查看>>
java基础题
查看>>
[转]人人店短信插件开发
查看>>
[转]c# System.IO.Ports SerialPort Class
查看>>
14. 最长公共前缀
查看>>
Redis文档
查看>>
项目重构
查看>>
(笔试题)和一半的组合数
查看>>
leetcode--Algorithm--Array_Part 1 Easy- 566 Reshape the Matrix
查看>>
AC自动机算法详解 (转载)
查看>>
python3-day5(模块)
查看>>