Cosmos DB: Querying documents properties with special characters such as "$" - azure

I've got a Cosmos DB collection with a document that contains properties that have a special character and what I assume is a reserved word. An example document is:
{
$type: 'Some value',
Value: 'Some other value'
}
If I execute the following query in the Azure Portal Query Explorer:
select * from c where c.Value = 'Some other value'
I receive an error of "Syntax error, incorrect syntax near 'Value'.". I get a similar error querying on c.$type.
How do I escape these property values so that I can query?

In the case of special characters, you will need to wrap the property inside []
Example:
SELECT * FROM c WHERE c["$type"] = "Some value"
SELECT * FROM c WHERE c["value"] = "$Some other value"

Related

Handling special characters in ARRAY_CONTAINS search in cosmos sql query

I have db structure like this :
"selfId": "cd29433e-f36b-1410-851b-009d805073d7",
"selfName" : "A",
"bookIds": [
"2f51bfd4",
"2f3a1010",
"090436c0",
"1078c3b2",
"b63b06e0"
]
I am working in C# and get bookId as a string.
I am writing query as :
string SQLquery = string.Format("select c.selfName from c where ARRAY_CONTAINS(c.bookIds,\""+bookId+"\"");
But When bookId contains special character, then query is giving error. For example if bookId = "AK"s" book" (please note id itself contains ") , then executing sqlQuery is giving error.
When a text contains quotes, slashes, special characters, you can use Escape Sequence
The quoted property operator [""] can also be used to access properties. for example, SELECT food.id and SELECT food["id"] are equal. This syntax can be used to escape a property with spaces, special characters, or a name that is the same as a SQL keyword or reserved term.
Example:
SELECT food["id"]
FROM food
WHERE food["foodGroup"] = "Snacks" and food["id"] = "19015"
Reference :- https://learn.microsoft.com/en-us/azure/cosmos-db/sql/sql-query-constants#bk_arguments

ADF: Pass dynamic Where Clause as a string with quotes

I have a lookup that retrieves a few records from a SQL Server table containing server, database, schema, table name and a whole where clause. These values are passed to a copy data (within a ForEach) In the copy data i have tried to use two different Dynamic query statement, but I seem to get an error. And can't figure out where I'm going wrong.
Values in table:
SRC_SERVERNAME
SRC_DATABASE
SRC_SCHEMANAME
SRC_TABLENAME
SRC_WHERE_DATE_CLAUSE
SQ01
NAV
dbo
Company$Sales Invoice Header
where [Posting Date] >= '2021-01-01'
Source setup:
Error for statement 1:
A database operation failed with the following error: 'Incorrect syntax near '.'.'
Incorrect syntax near '.'., SqlErrorNumber=102,Class=15,State=1,
Error for statement 2:
A database operation failed with the following error: 'Incorrect syntax near '.'.'
Incorrect syntax near '.'., SqlErrorNumber=102,Class=15,State=1,
Statement 1 (query):
SELECT *
FROM #{item().SRC_SERVERNAME}.#{item().SRC_DATABASENAME}.#{item().SRC_SCHEMANAME}.#{item().SRC_TABLENAME},' ',#{item().SRC_WHERE_DATE_CLAUSE}
Statement 2 (dynamic query with concat):
#concat('select * from ',item().SRC_SERVERNAME,'.',item().SRC_DATABASENAME,'.',item().SRC_SCHEMANAME,'.',item().SRC_TABLENAME,' ',item().SRC_WHERE_DATE_CLAUSE)
There is a syntax error in your query.
In the 4-part naming of SQL database, server name/database name/schema name/table name should be separated by ‘.’.
When you have space or other special characters in the name of server/database/schema/table, they should be embedded inside square braces [].
#concat('select * from [',item().SRC_SERVERNAME, '].[',item().SRC_DATABASENAME,'].[',item().SRC_SCHEMANAME,'].[',item().SRC_TABLENAME, '] ',item().SRC_WHERE_DATE_CLAUSE)

How to fix 'error: Error: syntax error - at value'

I have objects like:
address: {
"phone" : 888,
"value" : 12
}
And in WHERE i need to find objects by address.value, but in SQL there's function value(), so i always get an error.
I do it from node.js. Are there any variants to solve this without changing objects?
Sql request like:
SELECT count(*) as size FROM addresses WHERE address.value = 12
VALUE is a reserved word. If you want to use it as a common identifier, you need to enclose it in backticks:
SELECT count(*) as size FROM addresses WHERE address.`value` = 12

DocumentDB Stored Procedure Lumenize

I'm using the aggregate stored procedure lumenize https://github.com/lmaccherone/documentdb-lumenize with the .net client and I'm in trouble in the filterquery content.
How simply pass alphanumeric value to the filterquery query ?
string configString = #"{
cubeConfig: {
groupBy: 'Modele',
field: 'Distance',
f: 'sum'
},
filterQuery: 'SELECT * FROM Modele WHERE ModeleGUID = ''0b93def1-ccd7-fc35-0475-b47c89137c3f'' '}";
Each test gives me a parse error in the filterquery :(
Error: One or more errors occurred.
Message: After parsing a value an unexpected character was encountered:
'. Path 'filterQuery', line 7, position 63.
End of demo, press any key to exit.
Thanks
Just to properly close this out: The issue is related to multiple single-quotes in the filter string. As long as they're escaped properly (e.g. \'), things should work as expected.

In Azure DocumentDB some queries don't work

I used Azure DocumentDB to store some Key-Values pairs. This is the structure of the document I used
{
"Key": "Deleted",
"Value": {
"email": "abc#cdf.com"
}
}
When i write DocumentDB query like this,
SELECT C.Value FROM C
This query does not work. Here is the error message I get.
Syntax error, incorrect syntax near 'Value'.
But this query works fine,
SELECT C.Key FROM C
I understand 'Value' should be a keyword in azure documentdb. How can i query it?
Value is a keyword in DocumentDB syntax so this is why you get an error. See Value keyword in this article DocumentDB syntax - The VALUE keyword provides a way to return JSON value.
To get around this you can query it the way Yannick has said i.e.
SELECT C['Value'] FROM C
Because your documents are stored as JSON, you can access them as follows:
SELECT C['Value'] FROM C
This will result in:
[
{
"Value": {
"email": "abc#cdf.com"
}
}
]
In DocumentDB query grammar, the syntax < collection_expresion >.property_name is exactly same as < collection_expression >["property_name"]. So, effectively,
c.name
is same as
c["name"]
The syntax < collection_expression >["property_name"] allows one to derefence properties which might have non-identifier characters like 'my value' (space between my and value), or keywords like 'Value', 'Select' etc.
Hence,
SELECT C["Value"]
FROM C
will work in your case.

Resources