共用方式為


CosmosScripts.CreateStoredProcedureAsync Method

Definition

Creates a stored procedure as an asynchronous operation in the Azure Cosmos DB service.

public abstract System.Threading.Tasks.Task<Azure.Response<Azure.Cosmos.Scripts.StoredProcedureProperties>> CreateStoredProcedureAsync (Azure.Cosmos.Scripts.StoredProcedureProperties storedProcedureProperties, Azure.Cosmos.RequestOptions requestOptions = default, System.Threading.CancellationToken cancellationToken = default);
abstract member CreateStoredProcedureAsync : Azure.Cosmos.Scripts.StoredProcedureProperties * Azure.Cosmos.RequestOptions * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Azure.Response<Azure.Cosmos.Scripts.StoredProcedureProperties>>
Public MustOverride Function CreateStoredProcedureAsync (storedProcedureProperties As StoredProcedureProperties, Optional requestOptions As RequestOptions = Nothing, Optional cancellationToken As CancellationToken = Nothing) As Task(Of Response(Of StoredProcedureProperties))

Parameters

storedProcedureProperties
StoredProcedureProperties

The Stored Procedure to create

requestOptions
RequestOptions

(Optional) The options for the stored procedure request RequestOptions

cancellationToken
CancellationToken

(Optional) CancellationToken representing request cancellation.

Returns

The StoredProcedureProperties that was created contained within a Task object representing the service response for the asynchronous operation.

Exceptions

If storedProcedureProperties is not set.

Represents a consolidation of failures that occurred during async processing. Look within InnerExceptions to find the actual exception(s)

This exception can encapsulate many different types of errors. To determine the specific error always look at the StatusCode property. Some common codes you may get when creating a Document are:

StatusCodeReason for exception
400BadRequest - This means something was wrong with the request supplied. It is likely that an Id was not supplied for the stored procedure or the Body was malformed.
403Forbidden - You have reached your quota of stored procedures for the collection supplied. Contact support to have this quota increased.
409Conflict - This means a StoredProcedureProperties with an id matching the id you supplied already existed.
413RequestEntityTooLarge - This means the body of the StoredProcedureProperties you tried to create was too large.

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)
   {
       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]));
       });

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

CosmosScripts scripts = this.container.Scripts;
StoredProcedureProperties storedProcedure = new StoredProcedureProperties(id, sprocBody);
Response<StoredProcedureProperties> storedProcedureResponse = await scripts.CreateStoredProcedureAsync(storedProcedure);

// Execute the stored procedure
CosmosItemResponse<string> sprocResponse = await scripts.ExecuteStoredProcedureAsync<string, string>(
                              id, 
                              "Item as a string: ", 
                              new PartitionKey(testPartitionId));
Console.WriteLine("sprocResponse.Resource");

Applies to