Mssql reverse Query

Source: Internet
Author: User

Author: oldjun & emperor
Unfortunately, I got a database with hundreds of tables and N fields in many tables. I didn't want to manually detect useful tables and fields one by one and wrote some SQL statements, with the help of oldjun, we have this script. The function is to use a keyword as the search condition, traverse the entire database, and display all the locations of the keyword in the database, that is, the table name and column.

 

Directly give the code:


<Title> SQL reverse query </title>
<Form name = "Form" action = "? ">
<Input type = "text" name = "keyword" value = "Enter the keyword to search">
<Input type = "Submit" value = "Submit">
</Form>
<%
Search a keyword from each column in each table of a database
By oldjun (http://www.oldjun.com)
Based on huangzi (http://www.2chuizi.com) s SQL
Respect for the authors work

Server. ScriptTimeout = 999999999
Response. Buffer = true
On Error Resume Next
Keyword = request ("keyword ")
If keyword = "" then
Response. write "Need keyword! "
Response. End
End if
Dim conn
Set conn = Server. CreateObject ("ADODB. Connection ")
Dim ConnStr
ConnectionString, Pls change!
ConnStr = "Driver = {SQL SERVER}; Server = localhost; UID = bbs; PWD = bbs123456789; Database = syc163"
Conn. open ConnStr
Conn.exe cute ("drop table huangzi_table ")
Conn.exe cute ("create table huangzi_table (id int identity (1, 1), biaoid int, biaoname nvarchar (1000 ))")
Conn.exe cute ("insert huangzi_table select [id], [name] from sysobjects where xtype = U ")

Set rs limit conn.exe cute ("select count (id) as tnum from huangzi_table ")
Tnum = rs ("tnum ")
Rs. close
Set rs = nothing
For I = 1 to tnum

Set rsbiao into conn.exe cute ("select biaoid from huangzi_table where id =" & I &"")
Biaoid = rsbiao ("biaoid ")
Set rst into conn.exe cute ("select [biaoname] from huangzi_table where biaoid =" & biaoid &"")
Tname = rst ("biaoname ")

Set rsl=conn.exe cute ("select count ([name]) as lnum from syscolumns where id =" & biaoid &"")
Lnum = rsl ("lnum ")

For j = 1 to lnum

Topnum = J-1
Set rslieappsconn.exe cute ("select top 1 [name] from syscolumns where id =" & biaoid & "and [name] not in (select top" & topnum & "[name] from syscolumns where id = "& biaoid &") ")
Liename = rslie ("name ")
Set rsresult=conn.exe cute ("select top 1 [" & liename & "] from [" & tname & "] where CAST ([" & liename & "] as nvarchar (1000 )) = "& keyword &"")

If rsresult. bof or rsresult. eof then
Response. write "Nothing-" & tname & ":" & liename
Response. write "<br>"
Else
Result = rsresult (liename)
Response. write "table name:" & tname & "column name:" & liename & "keyword [" & keyword & "]"
Response. write "<br>"
End if

Rslie. close
Set rslie = nothing
Rsresult. close
Set rsresult = nothing
Next

Rsbiao. close
Set rsbiao = nothing
Rst. close
Set rst = nothing
Rsl. close
Set rsl = nothing
Next

Conn.exe cute ("drop table huangzi_table ")
%>

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.