次の方法で共有


CosmosScripts.ExecuteStoredProcedureAsync<TOutput> メソッド

定義

Azure Cosmos サービスの非同期操作としてコンテナーに対してストアド プロシージャを実行します。

public abstract System.Threading.Tasks.Task<Azure.Cosmos.Scripts.StoredProcedureExecuteResponse<TOutput>> ExecuteStoredProcedureAsync<TOutput> (string storedProcedureId, Azure.Cosmos.PartitionKey partitionKey, object[] parameters, Azure.Cosmos.StoredProcedureRequestOptions requestOptions = default, System.Threading.CancellationToken cancellationToken = default);
abstract member ExecuteStoredProcedureAsync : string * Azure.Cosmos.PartitionKey * obj[] * Azure.Cosmos.StoredProcedureRequestOptions * System.Threading.CancellationToken -> System.Threading.Tasks.Task<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))

型パラメーター

TOutput

JSON シリアル化可能な戻り値の型。

パラメーター

storedProcedureId
String

実行するストアド プロシージャの識別子。

partitionKey
PartitionKey

項目のパーティション キー。 PartitionKey

parameters
Object[]

(省略可能)ストアド プロシージャのパラメーターを表す動的オブジェクトの配列。

requestOptions
StoredProcedureRequestOptions

(省略可能)ストアド プロシージャ要求のオプション StoredProcedureRequestOptions

cancellationToken
CancellationToken

(省略可能) CancellationToken 要求の取り消しを表します。

戻り値

ストアド プロシージャに設定された応答を含む非同期操作のサービス応答を表すタスク オブジェクト。

例外

または partitionKey が設定されていない場合storedProcedureId

これにより、クエリから返される最初の項目に文字列を追加するストアド プロシージャが作成され、実行されます。

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."");
   }";

CosmosScripts 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);
/// 

適用対象