SQL Like Operator: Syntax and Usage
Note
This feature is only available in NCache Enterprise Edition.
NCache allows you to search for a specific pattern in a column through SQL like query format using LIKE operator.
The two wildcards used with the LIKE operator are:
*
: Used as a substitute for zero or more characters in the string.?
: Used as a substitute for a single character in the string.
Prerequisites
- To learn about the standard prerequisites required to work with all NCache client side features please refer to the given page on Client Side API Prerequisites.
- Searchable objects and their attributes must be indexed by either configuring indexes or defining indexes programmatically.
- The cache must be running.
- The application must be connected to cache before performing the operation.
- For API details, refer to: ICache, ICacheReader, ExecuteReader, SearchService, QueryCommand, ExecuteNonQuery.
- Make sure that the data being added is serializable.
- To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
- To handle any unseen exceptions, refer to the Troubleshooting section.
Syntax
Here's an example that searches the cache and retrieves all the products that have their ProductName starting with the letters Shamp, and Category starting with the letters Househo using ExecuteReader
.
try
{
// Pre-condition: Cache is already connected
// Items are already present in the cache
// Specify the query with the specified criteria.
// Use the Fully Qualified Name (FQN) of your own custom class
string query = "SELECT * FROM FQN.Product WHERE ProductName LIKE ? AND Category LIKE ?";
// Use QueryCommand for query execution
var queryCommand = new QueryCommand(query);
// Providing parameters for query
queryCommand.Parameters.Add("ProductName", "P*");
queryCommand.Parameters.Add("Category", "Beverage?");
// Executing QueryCommand through ICacheReader
ICacheReader reader = _cache.SearchService.ExecuteReader(queryCommand);
// Check if result set is not empty
if (reader.FieldCount > 0)
{
while (reader.Read())
{
// Get the value of the result set
string result = reader.GetValue<string>("ProductID");
// Perform operations
}
}
else
{
// Null query result set retrieved
}
}
catch (OperationFailedException ex)
{
if (ex.ErrorCode == NCacheErrorCodes.INCORRECT_FORMAT)
{
// Make sure that the query format is correct
}
else
{
// Exception can occur due to:
// Connection Failures
// Operation Timeout
// Operation performed during state transfer
}
}
catch (Exception ex)
{
// Any generic exception like ArgumentException, ArgumentNullException
}
Note
To ensure the operation is fail-safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
Additional Resources
NCache provides sample application for SQL Searching on GitHub.
See Also
Locking Data For Concurrency Control
SQL Search for Objects
SQL Search for Keys Syntax and Usage
SQL IN Operator Syntax and Usage
SQL GROUP BY Syntax and Usage
Query Operators
Search Cache with LINQ