SQL multi-condition query implementation methods

Source: Internet
Author: User

SQL multi-condition query implementation methods

SQL multi-condition Query

In the future, we will perform multi-condition queries. One is arrangement and combination, and the other is dynamic concatenation of SQL statements.

For example, we need two conditions: one is date @ addDate and the other is @ name.

The first method is

If (@ addDate is not null) and (@ name <> '') select * from table where addDate = @ addDate and name = @ nameelse if (@ addDate is not null) and (@ name = '') select * from table where addDate = @ addDate else if (@ addDate is null) and (@ name <> '') select * from table where and name = @ nameelse if (@ addDate is null) and (@ name = '') select * from table

The second type is dynamic composition of SQL, which is executed through exec and I will not write it,

Yesterday I thought of a way

Select * from table where (addDate = @ addDate or @ addDate is null) and (name = @ name or @ name = '')

Result 1: debugging is successful,

Please advise if you have any ideas and better methods !~

Thank you for reading this article. I hope it will help you. Thank you for your support for this site!

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.