ALL operator VS Any on an empty query
Because ANY
is to be interpreted as EXIST
(if there is any, it means they exist). Therefore, it return false if no rows are found.
All
does not certify that any values exist, it just certifies that it represents all possible values. Therefore, it return true even if no rows are found.
SQL query -- not getting expected result using ALL and '=' operator
I am expecting it to return rows with ids 1 and 2
You want any
, not all
:
where id = any ('{1,2}');
This brings id
that are either equal to 1
or 2
. In other words that's equivalent to id in (1, 2)
.
As regard to this expression:
where id <> all ('{3,4}');
This is equivalent to:
where not (id = any ('{3,4}'));
So this filters out id
s 3
and 4
.
Concerning your original expression:
where id = all ('{1,2}');
This does not make sense; a single value cannot be matched against all values of an array at once - so this filters out all rows.
Selecting all data if the string is empty in SQL IN operator
For this kind of "optional parameter" query, an option recompile
at the end can improve performance by quite a lot.
If an "unselected" parameter is an empty string, then you can do:
WHERE
(@brand_names = '' or tbl_product.brand_name IN (SELECT * from dbo.splitstring(@brand_names)))
and (@type = '' or tbl_product.filter_code IN (SELECT * from dbo.splitstring(@type)))
option (recompile)
The option (recompile)
tells SQL to build a new plan for this statement every time the procedure runs. So, for example, if you pass an empty string for @brand_names
, the engine doesn't even need to evaluate the or tbl_product.brand_name in ...
part of that predicate. If you don't do this, then SQL will - as always - build a plan for the first execution, and then reuse that plan on subsequent executions. That's not great when different parameter values can make such a big difference to the result.
EXISTS vs ALL, ANY, SOME
The queries in your question are not equivalent, so they will have different execution plans regardless of how well they're optimized. If you used NOT val > ANY(...)
then it would be equivalent.
You should always use EXPLAIN
to see the execution plan of a query and realize that the execution plan can change as your data changes. Testing and understanding the execution plan will help you determine which methods perform better. There is no hard and fast rule for ALL/ANY/SOME and they're often optimized down to an EXISTS.
Sql where clause, Get All if value is empty or null else use like statement
Your code can be updated to this:
SELECT [Category]
FROM [dbo].[Records]
WHERE (@SearchText IS NULL OR (Title like '%' + ISNULL( @SearchText ,'')+ '%'))
If you feed null
then first condition will be true
else second.
Related Topics
How to Find Elements in an Array in Bigquery
Database View Does Not Reflect The Data in The Underying Table
Apex 5.0: Show a Progress Bar While Database Action Is Performed
Sql Server 2012: Add a Linked Server to Postgresql
How to Add Months to a Current_Timestamp in Sql
Sql Server: Do I Need to Use Go Statements Between Batches
Adodataset Deleting from Joined Table
Crosstab Query with Dynamic Columns in SQL Server 2005 Up
Query to Get Content Older Than 3 Weeks
Return Value at Max Date for a Particular Id
How to Automatically Reset a Sequence's Value to 0 Every Year in Oracle 10G
How to Use Many Like Operators and Use Index
Handling Null in Greatest Function in Oracle
How to Split a String into Variables in Sql
Need to Convert Text Field to Varchar Temporarily So That I Can Pass to a Stored Procedure