• Webinars
  • Docs
  • Download
  • Blogs
  • Contact Us
Show / Hide Table of Contents

SQL Delete Statement Syntax and Usage

Note

This feature is only available in NCache Enterprise Edition.

Similar to SQL, NCache lets you delete the data from the cache based on the given criteria. The delete statement is executed through ExecuteNonQuery which returns the number of the deleted rows as a result of the query executed.

Note

The Delete statement can only be executed through ExecuteNonQuery:

Pre-Requisites

  • .NET/.NET Core
  • Java
  • Indexing for searchable objects and their attributes need to be configured first as explained in Configuring Query Indexes in Administrator's Guide.
  • Include the following namespaces in your application:
    • Alachisoft.NCache.Client
    • Alachisoft.NCache.Runtime.Caching
    • Alachisoft.NCache.Client.Services
    • Alachisoft.NCache.Runtime.Exceptions
  • The application must be connected to cache before performing the operation.
  • Cache must be running.
  • 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.
  • Include the following namespaces in your application:
    • import com.alachisoft.ncache.web.caching.*;
    • import com.alachisoft.ncache.runtime.exceptions.*;
  • The application must be connected to cache before performing the operation.
  • Cache must be running.
  • 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.

Modern Syntax

Here's an example which deletes all the products from the cache with the UnitsInStock equals to 0 using ExecuteNonQuery.

try
{
    // Pre-condition: Cache is already connected
    // Items are already present in the cache

    // Provide Fully Qualified Name (FQN) of your custom class 
    string query = "DELETE FROM FQN.Product WHERE UnitsInStock = 0";

    // Use QueryCommand for query execution
    var queryCommand = new QueryCommand(query);

    // Execute query
    cache.SearchService.ExecuteNonQuery(queryCommand);
}
catch (OperationFailedException ex)
{
    if (ex.ErrorCode == NCacheErrorCodes.INCORRECT_FORMAT)
    {
        // Make sure that the query format is correct
    }
    else
    {
        // Exception can occur due to:
        // Any other query executed except DELETE
        // Connection Failures
        // Operation Timeout
        // Operation performed during state transfer
    }
}
catch (Exception ex)
{
    // Any generic exception like ArgumentException, ArgumentNullException
}

Legacy Syntax

Note

Legacy API is only available in NCache Enterprise Edition.

The following example deletes all the products from the cache with the UnitsInStock equals to 0 using ExecuteNonQuery. For Java, the executeNonQuery method lets you search the cache for keys and objects fulfilling the query criteria.

  • .NET/.NET Core
  • Java
try
{
    // Pre-condition: Cache is already connected
    // Items are already present in the cache

    // Provide Fully Qualified Name (FQN) of your custom class
    string query = "DELETE FQN.Product WHERE this.UnitsInStock = ?";

    // Use QueryCommand for query execution
    var queryCommand = new QueryCommand(query);

    // Providing parameters for query
    queryCommand.Parameters.Add("UnitsInStock", 500);

    // Executing QueryCommand
    cache.ExecuteNonQuery(queryCommand);
}
catch (OperationFailedException ex)
{
    if (ex.ErrorCode == NCacheErrorCodes.INCORRECT_FORMAT)
    {
        // Make sure that the query format is correct
    }
    else
    {
        // Exception can occur due to:
        // Any other query executed except DELETE
        // Connection Failures
        // Operation Timeout
        // Operation performed during state transfer
    }
}
catch (Exception ex)
{
    // Any generic exception like ArgumentException, ArgumentNullException
}
try
{    
    // Pre-condition: Cache is already connected
    // Items are already present in the cache.

    // Create a query which will be executed on the data set
    // Use the Fully Qualified Name (FQN) of your own custom class
    String query = "DELETE FQN.Product WHERE this.ProductID > ?";

    // Providing parameters for query
    HashMap values = new HashMap();
    values.put("ProductID", 5);

    int result = cache.executeNonQuery(query, values);
    //returns number of effected rows.

}
catch (Exception ex) 
{
    // Exception can occur due to:
    // Connection Failures
    // Operation Timeout
    // Operation performed during state transfer
    // Any generic exception
}

Recommendation: 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 at:

  • GitHub
  • Shipped with NCache: %NCHOME%\samples\dotnet\SearchUsingSQL

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 Like Operator Syntax and Usage
Query Operators
Search Cache with LINQ

Back to top Copyright © 2017 Alachisoft