当前位置: 移动技术网 > IT编程>脚本编程>VBScript > SQL获取表结构的show_table.vbs (冰点极限NP)

SQL获取表结构的show_table.vbs (冰点极限NP)

2017年12月08日  | 移动技术网IT编程  | 我要评论
复制代码 代码如下:

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网