azureazure-cosmosdb

SqlQuerySpec object as a parameter for StoredProcedure in Azure DocumentDb


Can we pass a SqlQuerySpec object as a parameter to stored procedure in Document Db? I think this gives us flexibility to send parameterized SQL text and parameters to procedure. If this is not possible, I would like to know if it is possible to access the complete SQL from the SqlQuerySpec.


Solution

  • The server-side API takes the same JSON string as the REST and node.js APIs. The SqlQuerySpec type for the .NET SDK actually translates to this before sending it up when doing a regular query.

    So, here are two ways you can compose the parameterized query from within your stored procedure.

    1. In the package of parameters that you send to your stored procedure, include a JSON string like this:

      '''
      {      
          "query": "SELECT * FROM books b WHERE (b.Author.Name = @name)",     
          "parameters": [          
               {"name": "@name", "value": "Herman Melville"}         
          ] 
      }
      '''
      

    You may be able to pass the string directly into a call to Collection.queryDocuments() but you may have to do JSON.parse(<your string>) before calling Collection.queryDocuments()

    1. If you know the shape of your query is going to be the same for every call to this stored procedure, then you can send each parameter for your query in as a parameter for the stored procedure. You then have to compose the filterQuery object in the JavaScript of your stored procedure.