尽管 SQL 查询有效,但 Azure 门户中的 Azure 存储过程不起作用

标签 azure stored-procedures azure-cosmosdb

以下存储过程未能生成任何结果,即使其中使用的 SQL 查询在 Azure 门户上测试时生成了结果。

function checktemp() {
    var context = getContext();
    var container = context.getCollection();
    var response = context.getResponse();

    let query = `SELECT DISTINCT {"Elevator": t.connectiondeviceid,
        "Vibration": t["vibration"],
        "Temperature": t["temperature"]} 

    FROM t 
    WHERE t["temperature"] > 75
    AND t.EventEnqueuedUtcTime > "2019-08-03T20:30:51.905Z"
    ORDER BY t["temperature"] DESC`

    // Query documents and take 1st item.
    var isAccepted = container.queryDocuments(
        container.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) {
            response.setBody('no docs found');
        }
        else {
            var body = { moststrain: feed[0] };
            response.setBody(JSON.stringify(body));
        }
    });

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

我希望能退回元素,但我总是收到“未找到文档”的消息。我的分区键是/ConnectionDeviceId。

最佳答案

使用存储过程代码测试了示例文档,它对我有用。您的 SP 结构应该没问题。

enter image description here

您提供的属性(ConnectionDeviceId)拼写有误,在sql中应该是ConnectionDeviceId:t.ConnectionDeviceId

enter image description here

为了解决诸如门户中的某些内容在SP中没有结果之类的问题,我建议您逐步删除部分查询语句,以定位哪部分SQL导致没有结果。

<小时/>

无论如何,这个问题与分区键有关。当您在门户中查询数据时,它会扫描所有分区。但是,如果执行 SP,它只会扫描特定分区。

由于数据是在“connectiondeviceid”上分区的,因此我应该在存储过程执行期间提供它的值。

关于尽管 SQL 查询有效,但 Azure 门户中的 Azure 存储过程不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57353399/

相关文章:

python - 如何在 Azure ML Studio 中保存 ipython 笔记本中的数据集?

.net - Entity Framework 存储过程和 POCO

azure-cosmosdb - 从 CosmosDB 中文档嵌套数组中的对象中选择值

azure-cosmosdb - 如何在 Azure AD B2C 用户注册时将其保存到 Cosmos DB?

mysql 不想要变量分配后的结果

Azure Cosmos DB -- 创建容器后更新冲突解决策略

用于矢量 PDF 的 Azure 读取 API

authentication - 报告服务身份验证问题

c# - Peek 如何在启用分区的服务总线队列中工作?

mysql - 存储过程 VB.NET MYSQL