你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
Scripts.CreateStoredProcedureAsync 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
在 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 | 异常原因 |
---|---|
400 | BadRequest - 这意味着所提供的请求出错。 可能是没有为存储过程提供 ID,或者正文格式不正确。 |
403 | 禁止 - 已达到提供的集合的存储过程配额。 请联系支持人员以增加此配额。 |
409 | 冲突 - 这意味着 , StoredProcedureProperties 其 ID 与所提供的 ID 匹配。 |
413 | RequestEntityTooLarge - 这意味着尝试创建的 主体 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");