Sdílet prostřednictvím


Scripts.ExecuteStoredProcedureAsync<TOutput> Method

Definition

Executes a stored procedure against a container as an asynchronous operation in the Azure Cosmos service.

public abstract System.Threading.Tasks.Task<Microsoft.Azure.Cosmos.Scripts.StoredProcedureExecuteResponse<TOutput>> ExecuteStoredProcedureAsync<TOutput> (string storedProcedureId, Microsoft.Azure.Cosmos.PartitionKey partitionKey, object[] parameters, Microsoft.Azure.Cosmos.Scripts.StoredProcedureRequestOptions requestOptions = default, System.Threading.CancellationToken cancellationToken = default);
abstract member ExecuteStoredProcedureAsync : string * Microsoft.Azure.Cosmos.PartitionKey * obj[] * Microsoft.Azure.Cosmos.Scripts.StoredProcedureRequestOptions * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Microsoft.Azure.Cosmos.Scripts.StoredProcedureExecuteResponse<'Output>>
Public MustOverride Function ExecuteStoredProcedureAsync(Of TOutput) (storedProcedureId As String, partitionKey As PartitionKey, parameters As Object(), Optional requestOptions As StoredProcedureRequestOptions = Nothing, Optional cancellationToken As CancellationToken = Nothing) As Task(Of StoredProcedureExecuteResponse(Of TOutput))

Type Parameters

TOutput

The return type that is JSON serializable.

Parameters

storedProcedureId
String

The identifier of the Stored Procedure to execute.

partitionKey
PartitionKey

The partition key for the item.

parameters
Object[]

(Optional) An array of dynamic objects representing the parameters for the stored procedure.

requestOptions
StoredProcedureRequestOptions

(Optional) The options for the stored procedure request.

cancellationToken
CancellationToken

(Optional) CancellationToken representing request cancellation.

Returns

The task object representing the service response for the asynchronous operation which would contain any response set in the stored procedure.

Exceptions

If storedProcedureId or partitionKey are not set.

Examples

This creates and executes a stored procedure that appends a string to the first item returned from the query.

string sprocBody = @"function simple(prefix, postfix)
   {
       var collection = getContext().getCollection();

       // Query documents and take 1st item.
       var isAccepted = collection.queryDocuments(
       collection.getSelfLink(),
       'SELECT * FROM root r',
       function(err, feed, options) {
           if (err)throw err;

           // Check the feed and if it's empty, set the body to 'no docs found',
           // Otherwise just take 1st element from the feed.
           if (!feed || !feed.length) getContext().getResponse().setBody(""no docs found"");
           else getContext().getResponse().setBody(prefix + JSON.stringify(feed[0]) + postfix);
       });

       if (!isAccepted) throw new Error(""The query wasn't accepted by the server. Try again/use continuation token between API and script."");
   }";

Scripts scripts = this.container.Scripts;
string sprocId = "appendString";
StoredProcedureResponse storedProcedureResponse = await scripts.CreateStoredProcedureAsync(
        sprocId,
        sprocBody);

// Execute the stored procedure
StoredProcedureExecuteResponse<string> sprocResponse = await scripts.ExecuteStoredProcedureAsync<string>(
                        sprocId,
                        new PartitionKey(testPartitionId),
                        new dynamic[] {"myPrefixString", "myPostfixString"});

Console.WriteLine(sprocResponse.Resource);
/// 

Applies to