SQL command to obtain show_table.vbs of the table structure (freezing point limit NP)

Source: Internet
Author: User

Copy codeThe Code is as follows: 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.exe cute ("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.exe cute (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 orders where table_name = '" & table_name & "'order by ordinal_position"
'Wsh. echo sql2

Set rs21_conn.exe cute (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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.