使用OFFSET子句进行参数化查询

时间:2019-06-05 16:42:12

标签: azure-cosmosdb azure-cosmosdb-sqlapi

我使用了以下存储过程,并尝试通过传递number参数来对查询进行参数化。但是,它在执行存储过程时给了我错误。任何见解真的很有帮助

function uspGetUsersByPage(number) {
  //Set Environment
  let context = getContext();
  let coll = context.getCollection();
  let link = coll.getSelfLink();
  let response = context.getResponse(); 

  let query = {
    query: 'SELECT * FROM a WHERE a.DocumentType = "UserRole" and a.AuditFields.IsLatest = true and a.AuditFields.IsDeleted = false OFFSET @number LIMIT 20'
    , parameters: [{ name: '@number', value: number }]
  };


  //Execute the query against the collection
  let runquery = coll.queryDocuments(link, query, {}, callbackfn);

  //Call function to throw an error(if any) or display the output
  function callbackfn(err, queryoutput) {
    if (err) {
      throw err;
    }

    if (!queryoutput || !queryoutput.length) {
      response.setBody(null);
    }
    else {
      response.setBody(queryoutput);
    }
  };

  //Display standard output if query doesnt get any results
  if (!runquery) { throw Error('Unable to retrieve requested information'); }
};

1 个答案:

答案 0 :(得分:0)

请按照您的描述查看我的简单测试。

数据:

enter image description here

存储过程:

function sample(prefix) {
    var collection = getContext().getCollection();
    var query =  {query: "SELECT c.id,c.number FROM c offset @num limit 1", parameters: 
[{name: "@num", value: prefix}]};

   console.log(query);
    var isAccepted = collection.queryDocuments(
        collection.getSelfLink(),
        query,
    function (err, feed, options) {
        if (err) throw err;

        // Check the feed and if empty, set the body to 'no docs found', 
        // else take 1st element from feed
        if (!feed || !feed.length) {
            var response = getContext().getResponse();
            response.setBody('no docs found');
        }
        else {
            var response = getContext().getResponse();   
            response.setBody(feed);
        }
    });

    if (!isAccepted) throw new Error('The query was not accepted by the server.');
}

输入:

enter image description here

输出:

enter image description here