The most concise multi-query solution under ASP

Source: Internet
Author: User

The most concise multi-query Solution

[Preface]
We often encounter multiple query problems, and long SQL statements are often confusing. Especially when the client partially fills in the query conditions, it is even more difficult to use common methods.
The following cleverly uses the "where 1 = 1" equation (in fact many, let it be set to true) to solve this problem.

[Body Summary]

'Subject information title
Name of the company whose company information is published
'Content content
'Address company address
'Infomation Company Profile
'Note
The preceding values are submitted by form and then obtained through: Subject = trim (request. Form ("subject.

<%
'The function is critical! ---------------------------
Function SQL (a, B, sqls)
If B <> "" then', if the client does not submit this value, no corresponding SQL statement is 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. recordset ")
Sqls = "select * From mytable where 1 = 1"
'Call the above function below, and you can call multiple (theoretically any)
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. Open sqls, Conn, 3, 2
%>

[Conclusion]
If there is no key function SQL (a, B, sqls), we can imagine the number of statements that need to be judged one by one!

Related Article

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.