次の方法で共有


Scripts.CreateStoredProcedureAsync メソッド

定義

Azure Cosmos DB サービスで非同期操作としてストアド プロシージャを作成します。

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

パラメーター

storedProcedureProperties
StoredProcedureProperties

作成するストアド プロシージャ。

requestOptions
RequestOptions

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

cancellationToken
CancellationToken

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

戻り値

非同期操作の StoredProcedureProperties サービス応答を Task 表す オブジェクト内に格納されている 作成された 。

例外

が設定されていない場合 storedProcedureProperties

非同期処理中に発生したエラーの統合を表します。 InnerExceptions 内を見て、実際の例外を見つけます

この例外は、さまざまな種類のエラーをカプセル化できます。 特定のエラーを特定するには、常に StatusCode プロパティを参照してください。 ドキュメントの作成時に取得できる一般的なコードは次のとおりです。

StatusCode例外の理由
400BadRequest - これは、指定された要求に何らかの問題が発生したことを意味します。 ストアド プロシージャに ID が指定されていないか、本文の形式が正しくない可能性があります。
403禁止 - 指定されたコレクションのストアド プロシージャのクォータに達しました。 このクォータを増やすには、サポートにお問い合わせください。
409競合 - これは、指定した ID と一致する ID が既に存在する を意味 StoredProcedureProperties します。
413RequestEntityTooLarge - 作成しようとしたの本文が StoredProcedureProperties 大きすぎたということです。

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

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

Scripts scripts = this.container.Scripts;
StoredProcedureProperties storedProcedure = new StoredProcedureProperties(id, sprocBody);
StoredProcedureResponse 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");

適用対象