如何在 Azure Cosmos DB SQL API 中使用 SQL 参数进行 CONTAINS 查询?
How do I use SQL Parameter for CONTAINS query in Azure Cosmos DB SQL API?
我有以下 sql 变量:
SELECT * FROM x WHERE x.Description CONTAINS(@p1)
我已经配置了一个名为@p1 的 SQL 参数和一个有效的字符串,即 "foo" 并将其添加到 SqlParameterCollection 集合变量中,如下所示:
var sqlQuery = new SqlQuerySpec(sql, collection);
然后我调用以下查询。
var query = _documentClient.CreateDocumentQuery<T>(GetDocumentCollectionUri<T>(graphRequestContext), sqlQuery, options).AsDocumentQuery();
我收到以下错误:
消息:Microsoft.Azure.Documents.DocumentClientException : 消息:{"errors":[{"severity":"Error","location":{"start":37 ,"end":45},"code":"SC1001","message":"Syntax error, incorrect syntax near 'CONTAINS'."}]}
ActivityId:<出于隐私原因删除>,Microsoft.Azure.Documents.Common/2.2.0.0,Windows/10.0.17763 documentdb-netcore-sdk/2.2.2
根据文档 (https://docs.microsoft.com/en-us/azure/cosmos-db/sql-api-query-reference),它似乎在寻找字符串表达式。因此,如果确实如此,我正在寻找对此的确认。
看来你用错了CONTAINS
syntax.Please了解一下correct syntax。
请参考下面的代码,它适用于我。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using System;
namespace DocumentDB.TestClass
{
class CreateBySqlParam
{
private static readonly string endpointUrl = "https://***.documents.azure.com:443/";
private static readonly string authorizationKey = "***";
private static readonly string databaseId = "db";
private static readonly string collectionId = "***";
private static DocumentClient client;
public static async void QueryTest()
{
client = new DocumentClient(new Uri(endpointUrl), authorizationKey);
var uri = UriFactory.CreateDocumentCollectionUri(databaseId, collectionId);
IQueryable<Pojo> queryable = client.CreateDocumentQuery<Pojo>(
uri,
new SqlQuerySpec
{
QueryText = "SELECT c.id,c.name FROM c WHERE contains(c.name,@param)",
Parameters = new SqlParameterCollection()
{
new SqlParameter("@param", "n")
}
}
);
foreach (Pojo p in queryable)
{
Console.WriteLine("\nRead {0}", p);
}
}
}
class Pojo : Document
{
public string id { get; set; }
public string name { get; set; }
}
}
输出:
我有以下 sql 变量:
SELECT * FROM x WHERE x.Description CONTAINS(@p1)
我已经配置了一个名为@p1 的 SQL 参数和一个有效的字符串,即 "foo" 并将其添加到 SqlParameterCollection 集合变量中,如下所示:
var sqlQuery = new SqlQuerySpec(sql, collection);
然后我调用以下查询。
var query = _documentClient.CreateDocumentQuery<T>(GetDocumentCollectionUri<T>(graphRequestContext), sqlQuery, options).AsDocumentQuery();
我收到以下错误:
消息:Microsoft.Azure.Documents.DocumentClientException : 消息:{"errors":[{"severity":"Error","location":{"start":37 ,"end":45},"code":"SC1001","message":"Syntax error, incorrect syntax near 'CONTAINS'."}]} ActivityId:<出于隐私原因删除>,Microsoft.Azure.Documents.Common/2.2.0.0,Windows/10.0.17763 documentdb-netcore-sdk/2.2.2
根据文档 (https://docs.microsoft.com/en-us/azure/cosmos-db/sql-api-query-reference),它似乎在寻找字符串表达式。因此,如果确实如此,我正在寻找对此的确认。
看来你用错了CONTAINS
syntax.Please了解一下correct syntax。
请参考下面的代码,它适用于我。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using System;
namespace DocumentDB.TestClass
{
class CreateBySqlParam
{
private static readonly string endpointUrl = "https://***.documents.azure.com:443/";
private static readonly string authorizationKey = "***";
private static readonly string databaseId = "db";
private static readonly string collectionId = "***";
private static DocumentClient client;
public static async void QueryTest()
{
client = new DocumentClient(new Uri(endpointUrl), authorizationKey);
var uri = UriFactory.CreateDocumentCollectionUri(databaseId, collectionId);
IQueryable<Pojo> queryable = client.CreateDocumentQuery<Pojo>(
uri,
new SqlQuerySpec
{
QueryText = "SELECT c.id,c.name FROM c WHERE contains(c.name,@param)",
Parameters = new SqlParameterCollection()
{
new SqlParameter("@param", "n")
}
}
);
foreach (Pojo p in queryable)
{
Console.WriteLine("\nRead {0}", p);
}
}
}
class Pojo : Document
{
public string id { get; set; }
public string name { get; set; }
}
}
输出: