Scripts.CreateStoredProcedureStreamAsync Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Creates a stored procedure as an asynchronous operation in the Azure Cosmos DB service.
public abstract System.Threading.Tasks.Task<Microsoft.Azure.Cosmos.ResponseMessage> CreateStoredProcedureStreamAsync(Microsoft.Azure.Cosmos.Scripts.StoredProcedureProperties storedProcedureProperties, Microsoft.Azure.Cosmos.RequestOptions requestOptions = default, System.Threading.CancellationToken cancellationToken = default);
abstract member CreateStoredProcedureStreamAsync : Microsoft.Azure.Cosmos.Scripts.StoredProcedureProperties * Microsoft.Azure.Cosmos.RequestOptions * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Microsoft.Azure.Cosmos.ResponseMessage>
Public MustOverride Function CreateStoredProcedureStreamAsync (storedProcedureProperties As StoredProcedureProperties, Optional requestOptions As RequestOptions = Nothing, Optional cancellationToken As CancellationToken = Nothing) As Task(Of ResponseMessage)
Parameters
- storedProcedureProperties
- StoredProcedureProperties
The Stored Procedure to create.
- requestOptions
- RequestOptions
- cancellationToken
- CancellationToken
Returns
A Task containing a ResponseMessage containing the created stored procedure
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."");
}";
Scripts scripts = this.container.Scripts;
StoredProcedureProperties storedProcedure = new StoredProcedureProperties(id, sprocBody);
ResponseMessage storedProcedureResponse = await scripts.CreateStoredProcedureStreamAsync(storedProcedure);
// Execute the stored procedure
ResponseMessage sprocResponse = await scripts.ExecuteStoredProcedureStreamAsync(
sprocId,
new PartitionKey(testPartitionId),
new dynamic[] {"myPrefixString", "myPostfixString"});
if (!response.IsSuccessStatusCode)
{
//Handle and log exception
return;
}
using (StreamReader sr = new StreamReader(sprocResponse.Content))
{
string stringResponse = await sr.ReadToEndAsync();
Console.WriteLine(stringResponse);
}