資料字典到SQL語句的轉換 使用word與VBA

2021-09-07 23:59:23 字數 3888 閱讀 4223

一般我們有幾種方式對資料庫建模:

1.採用powerdesign或erwin等建模工具進行建模

2.利用word或excel進行建模

使用第一種方式,可以自動匯出要生產表的sql語句,建表比較方便,而對於第二種方式,則可能需要手工建表,其實在word中可以用vba完成對sql語句的生成,如將下面格式的word資料字典生成sql語句,就可以利用vba進行

word文件樣式為:

table1

pk預設值

field1

varchar(50)

是field2

intfield3

intfield4

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)

iftrim(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

" & ","

endif

sresult = sresult & stemp & "

" & stemp2 & vbcrlf

endif

next j

sresult = shead & vbcrlf & sresult

next i

endif

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 '

首先刪除原始檔

'開啟檔案

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

資料字典的匯出sql語句

select c.table name 表明,t.table comment 表備註,c.column name 列名,c.column type 資料型別,c.data type 字段型別,c.character maximum length 長度,c.is nullable 是否為空,c.col...

SQL資料字典

set ansi nulls on set quoted identifier on goalter procedure dbo proc dataselect tabelname varchar 100 asselect tablename o.name,fieldid c.colid,field...

通過sql語句得到資料字典(zz)

select 表名 case when a.colorder 1 then d.name else end,字段序號 a.colorder,欄位名 a.name,標識 case when columnproperty a.id,a.name,isidentity 1 then else end,主鍵...