以下存储过程未能生成任何结果,即使其中使用的 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 结构应该没问题。
您提供的属性(ConnectionDeviceId
)拼写有误,在sql中应该是ConnectionDeviceId
:t.ConnectionDeviceId
。
为了解决诸如门户中的某些内容在SP中没有结果之类的问题,我建议您逐步删除部分查询语句,以定位哪部分SQL导致没有结果。
<小时/>无论如何,这个问题与分区键有关。当您在门户中查询数据时,它会扫描所有分区。但是,如果执行 SP,它只会扫描特定分区。
由于数据是在“connectiondeviceid”上分区的,因此我应该在存储过程执行期间提供它的值。
关于尽管 SQL 查询有效,但 Azure 门户中的 Azure 存储过程不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57353399/