We often encounter multiple query problems, and long SQL statements often make people feel confused. Especially when the client is partially filled in the query conditions, it will be more difficult to use ordinary methods.
The following cleverly uses the constant formation of where 1 = 1 (in fact, so that its value is TRUE) to solve this problem.
Summary:
'Subject information title
'Company published information company name
'Content publishing information content
'address company address
Introduction to 'Infomation
'Note related instructions
The above values are submitted by Form, and then passed: subject = trim (request.form (subject)), etc. to obtain the corresponding value.
<%
'This function is critical!
Function SQL (A, B, SQLS)
If B <> THen 'If the client does not submit this value, the corresponding SQL statement will not be generated.
sqls = SQLS & AND & A & Like ' % & B & %'
end if
SQL = SQLS
End function
'----------------- call the database
Set conn = server.createObject (adodb.connection)
Dbpath = server.mappath (/database/mydb.mdb)
Conn.open driver = {microSoft Access Driver (*.mdb)}; pwd =; dbq = & dbpath
Set rs = server.createObject (Adodb.oldSet)
sqls = select * from mytable where 1 = 1
'The following calls can be called above, and many calls can be called (theoretically arbitrarily)
SQLS = SQL (Subject, Subject, SQLS)
sqls = SQL (Company, Company, SQLS)
sqls = sql (content, content, sqls)
sqls = SQL (address, address, sqls)
sqls = SQL (Infomation, Infomation, SQLS)
sqls = SQL (Note, Note, SQLS)
SQLS = SQLS & Order by ID DESC
RS.Oopen SQLS, CONN, 3,2
%>
Conclusion: If there is no key function Function SQL (A, B, SQLS), we can imagine how many judgments need to judge one after another!