溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何實(shí)現(xiàn)SQL獲取表結(jié)構(gòu)的show_table.vbs

發(fā)布時(shí)間:2021-10-11 10:32:04 來源:億速云 閱讀:99 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要介紹如何實(shí)現(xiàn)SQL獲取表結(jié)構(gòu)的show_table.vbs,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

 代碼如下:


set arg=wscript.arguments
If arg.count = 0 Then
show_help()
wsh.quit
End If
Server = arg(0)
User = arg(1)
pass = arg(2)
database_name = arg(3)
table_name = arg(4)
set Conn = CreateObject("ADODB.Connection")
Conn.Open "Driver={SQL Server};Server=" & Server & ";UID=" & User & ";PWD=" & Pass & ";Database=" & database_name
Set rs = conn.execute("select count(*) as n from " & database_name & ".dbo.sysobjects where id = object_id(N'[dbo].[" & table_name & "]')")
if rs("n") <=0 Then
wsh.echo "table have???"
wsh.quit
End If
sql1="use " &database_name & ";select count(column_name) from information_schema.columns where table_name='" & table_name & "'"
set rs1=conn.execute(sql1)
num = rs1(0)
rs1.close
Set rs1 = Nothing
i = 1
sql2="use " &database_name & ";select column_name,data_type,IS_NULLABLE,character_octet_length,collation_name,domain_name from information_schema.columns where table_name='" & table_name & "' order by ordinal_position"
'wsh.echo sql2

set rs2=conn.execute (sql2)
wsh.echo "if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[" & table_name & "]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)"
wsh.echo "drop table [dbo].[" & table_name & "]"
wsh.echo "GO"
wsh.echo "CREATE TABLE [dbo].[" & table_name & "] ("
do while not rs2.eof
col_dn = rs2("domain_name")
col_name = rs2("column_name")
col_type = rs2("data_type")
col_len = rs2("character_octet_length")
col_an = rs2("collation_name")
col_is = rs2("IS_NULLABLE")
sql = chr(9)&"[" & col_name & "] "
If col_dn = "id" Then
sql = sql & col_dn &" "
Else

sql = sql & "[" & col_type & "] "
If col_len <> "" Then
sql = sql & "(" & col_len & ") "
End If
If col_an <> "" Then
sql = sql & "COLLATE " & col_an
End If
End If


If col_is = "No" Then
sql = sql & " NOT NULL "
Else
sql = sql & " NULL "
End If
If i = num Then
sql = sql & Chr(13)&Chr(10)&") ON [PRIMARY]"
Else
sql = sql & ","
End If
i = i +1
wsh.echo sql
rs2.movenext
loop
wsh.echo "GO"
rs2.close
Set rs2 = Nothing
conn.close
Set conn=Nothing
Sub show_help()
wsh.echo "code by N37P47ch "
wsh.echo "cscript show_table.vbs ip user pass database table"
End Sub

以上是“如何實(shí)現(xiàn)SQL獲取表結(jié)構(gòu)的show_table.vbs”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI