News Feed

No results for 'undefined'
Powered by Algolia

How to pass container name as a parameter for Azure cosmos DB query in stored procedure?


Author: ramakumarja

Originally Sourced from: https://stackoverflow.com/questions/64136721/how-to-pass-container-name-as-a-parameter-for-azure-cosmos-db-query-in-stored-pr

I'm trying to generalize a javascript stored procedure, so that it can be run on any of the cosmos db containers in the database. Currently my query looks like this:

'select * from user r where r.id = @id', parameters: [{name: '@id', value: update.id}]

and this is working fine. Now I would like to pass 'user' which is a container as a parameter input, something like:

'select * from @container r where r.id = @id', parameters: [{name: "@container", value: update.container}, {name: '@id', value: update.id}]

This throws an error saying it's a syntax error.

(node:93270) UnhandledPromiseRejectionWarning: Error: Message: {"Errors":["Encountered exception while executing function. Exception = Error: {\"errors\":[{\"severity\":\"Error\",\"location\":{\"start\":14,\"end\":24},\"code\":\"SC1001\",\"message\":\"Syntax error, incorrect syntax near '@container'.\"}]}\r\nStack trace: Error: {\"errors\":[{\"severity\":\"Error\",\"location\":{\"start\":14,\"end\":24},\"code\":\"SC1001\",\"message\":\"Syntax error, incorrect syntax near '@container'.\"}]}\n   at Anonymous function (script.js:1:805)\n   at Anonymous function (script.js:614:29)"]}

Could anybody help me understand how to make this work? Thank you in advance.