This article is a mirror article of machine translation, please click here to jump to the original article.

View: 10053|Reply: 0

[Source] MSSQL conditional query WHERE ignores the condition

[Copy link]
Posted on 7/6/2020 10:37:56 AM | | | |
Requirements: We have a database table with a name field, and when the name is passed in is null, ignore this condition. There is also an age field, which also ignores this condition when 0 is passed.
Create a temporary test table and insert the data with the following code:



Test queries with ages of 0 and non-0 as follows:

There is also a way to write:



It can be seen that when we pass in a non-0, we will query the data that meets the requirements according to the conditions, and when it is 0, we will ignore the condition and query all.



Test the case where the name is null and non-null as follows:

Equivalent to



(End)




Previous:.Net/C# Selenium automated tests get cookie values
Next:SQL Server returns the primary key after insertion
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com