SQL examples for SQL Server
The ZappySys API Driver is a user-friendly interface designed to facilitate the seamless integration of various applications with the Cosmos DB API. With its intuitive design and robust functionality, the ZappySys API Driver simplifies the process of configuring specific API endpoints to efficiently read or write data from Cosmos DB.
On this page you will find some SQL examples which can be used for API ODBC Driver or Data Gateway API Connector.
Query documents (default container)
DECLARE @MyQuery NVARCHAR(MAX) = '#DirectSQL SELECT * FROM root where root.id !=null order by root._ts desc';
EXEC (@MyQuery) AT [LS_TO_COSMOS_DB_IN_GATEWAY];
Learn more about this SQL query.
List all documents in a container
DECLARE @MyQuery NVARCHAR(MAX) = 'SELECT * FROM TestContainer';
EXEC (@MyQuery) AT [LS_TO_COSMOS_DB_IN_GATEWAY];
Learn more about this SQL query.
Get document by ID
DECLARE @MyQuery NVARCHAR(MAX) = 'SELECT * FROM TestContainer Where Id=''user2''';
EXEC (@MyQuery) AT [LS_TO_COSMOS_DB_IN_GATEWAY];
Learn more about this SQL query.
Query documents with Cosmos DB SQL
DECLARE @MyQuery NVARCHAR(MAX) = 'SELECT *
FROM query_documents
WITH(
-- Database=''TestDB'', --if you dont supply connection level Default Database name is used
Table=''TestContainer'',
Query=''select * from root Where root.id!=null order by root._ts desc'',
Meta=''id:string(50);name:string(50);city;age''
--Meta=''id; name; city; age'' -- no types at all. Default is string(2000)
--Meta=''id; name:string(50); city; age: int'' --Mixed types. If type is missing default string(2000) used
--check below URL for more information on Query Language Syntax
--https://learn.microsoft.com/en-us/azure/cosmos-db/nosql/query/select
)';
EXEC (@MyQuery) AT [LS_TO_COSMOS_DB_IN_GATEWAY];
Learn more about this SQL query.
Create document with partition key
DECLARE @MyQuery NVARCHAR(MAX) = 'INSERT INTO TestContainer (PartitionKey, Document)
VALUES(
''["user2"]'', --partition key value must match its attribute from document else it will throw error. In this example container PartitionKey is /id so we used its value. For multiple key use JSON array ["val1","val2"]
''{
"id": "user2",
"name": "John Doe",
"email": "jdoe@contoso.com",
"phone": ["12345"],
"level": "platinum"
}''
)
WITH(Upsert=''true'')';
EXEC (@MyQuery) AT [LS_TO_COSMOS_DB_IN_GATEWAY];
Learn more about this SQL query.
Create document from file path
DECLARE @MyQuery NVARCHAR(MAX) = 'INSERT INTO TestContainer (PartitionKey, Document)
VALUES(
''["user2"]'', --partition key value must match its attribute from document else it will throw error. In this example container PartitionKey is /id so we used its value. For multiple key use JSON array ["val1","val2"]
''@c:\data\order.json'' --path must start with @ symbol
)
WITH(Upsert=''true'', IsMultiPart=1)';
EXEC (@MyQuery) AT [LS_TO_COSMOS_DB_IN_GATEWAY];
Learn more about this SQL query.
Upsert document (insert or update if exists)
DECLARE @MyQuery NVARCHAR(MAX) = 'INSERT INTO TestContainer (PartitionKey, Document)
VALUES(
''["user2"]'', --partition key value must match its attribute from document else it will throw error. In this example container PartitionKey is /id so we used its value. For multiple key use JSON array ["val1","val2"]
''{
"id": "user2",
"name": "John Doe",
"email": "jdoe@contoso.com",
"phone": ["12345"],
"level": "platinum"
}'')
WITH(Upsert=''true'')';
EXEC (@MyQuery) AT [LS_TO_COSMOS_DB_IN_GATEWAY];
Learn more about this SQL query.
Upsert document using UPSERT keyword
DECLARE @MyQuery NVARCHAR(MAX) = 'UPSERT INTO TestContainer (PartitionKey, Document)
VALUES(
''["user2"]'', --partition key value must match its attribute from document else it will throw error. In this example container PartitionKey is /id so we used its value. For multiple key use JSON array ["val1","val2"]
''{
"id": "user2",
"name": "John Doe",
"email": "jdoe@contoso.com",
"phone": ["12345"],
"level": "platinum"
}'')';
EXEC (@MyQuery) AT [LS_TO_COSMOS_DB_IN_GATEWAY];
Learn more about this SQL query.
Update document (full replace)
DECLARE @MyQuery NVARCHAR(MAX) = 'UPDATE TestContainer
SET
PartitionKey=''["user2"]''
,Document=
''{
"id" : "user2",
"name": "John Doe at <<FUN_NOW>>",
"email": "jdoe@contoso.com",
"phone": ["<<FUN_TODAY>>"],
"level": "platinum"
}''
Where Id=''user2''';
EXEC (@MyQuery) AT [LS_TO_COSMOS_DB_IN_GATEWAY];
Learn more about this SQL query.
Partial update document (PATCH)
DECLARE @MyQuery NVARCHAR(MAX) = 'UPDATE TestContainer
SET
PartitionKey=''["user2"]''
,Document=
''{
"operations": [
{ "op": "set", "path": "/name", "value": "updated name" }
,{ "op": "set", "path": "/email", "value": "updated@email.com" }
]
}''
Where Id=''user2''
WITH(
RequestMethod=''PATCH'' --Partial Replace (change name and email only)
)
/*
Example Document Operations
https://learn.microsoft.com/en-us/azure/cosmos-db/partial-document-update
https://learn.microsoft.com/en-us/rest/api/cosmos-db/patch-a-document
[
{ "op": "add", "path": "/color", "value": "silver" },
{ "op": "remove", "path": "/used" },
{ "op": "set", "path": "/price", "value": 355.45 }
{ "op": "incr", "path": "/inventory/quantity", "value": 10 },
{ "op": "add", "path": "/tags/-", "value": "featured-bikes" },
{ "op": "move", "from": "/color", "path": "/inventory/color" }
]
*/';
EXEC (@MyQuery) AT [LS_TO_COSMOS_DB_IN_GATEWAY];
Learn more about this SQL query.
Partial update document (PATCH) from file
DECLARE @MyQuery NVARCHAR(MAX) = 'UPDATE TestContainer
SET
PartitionKey=''["user2"]''
,Document=''@c:\temp\b.txt'' --path must start with @ symbol
Where Id=''user2''
WITH(
RequestMethod=''PATCH'' --Partial Replace (change name and email only)
,IsMultiPart=1 --this enables file upload
)
/*
Example Document Operations
https://learn.microsoft.com/en-us/azure/cosmos-db/partial-document-update
https://learn.microsoft.com/en-us/rest/api/cosmos-db/patch-a-document
[
{ "op": "add", "path": "/color", "value": "silver" },
{ "op": "remove", "path": "/used" },
{ "op": "set", "path": "/price", "value": 355.45 }
{ "op": "incr", "path": "/inventory/quantity", "value": 10 },
{ "op": "add", "path": "/tags/-", "value": "featured-bikes" },
{ "op": "move", "from": "/color", "path": "/inventory/color" }
]
*/';
EXEC (@MyQuery) AT [LS_TO_COSMOS_DB_IN_GATEWAY];
Learn more about this SQL query.
List databases
DECLARE @MyQuery NVARCHAR(MAX) = 'SELECT * FROM SysDatabases';
EXEC (@MyQuery) AT [LS_TO_COSMOS_DB_IN_GATEWAY];
Learn more about this SQL query.
List containers (tables)
DECLARE @MyQuery NVARCHAR(MAX) = 'SELECT * FROM SysTables';
EXEC (@MyQuery) AT [LS_TO_COSMOS_DB_IN_GATEWAY];
Learn more about this SQL query.
List containers for a database
DECLARE @MyQuery NVARCHAR(MAX) = 'SELECT * FROM SysTables WITH (Database=''MyCosmos DB'')';
EXEC (@MyQuery) AT [LS_TO_COSMOS_DB_IN_GATEWAY];
Learn more about this SQL query.
List users
DECLARE @MyQuery NVARCHAR(MAX) = 'SELECT * FROM SysUsers';
EXEC (@MyQuery) AT [LS_TO_COSMOS_DB_IN_GATEWAY];
Learn more about this SQL query.
Getting Started with Examples
ZappySys API Driver is a powerful software solution designed to facilitate the extraction and integration of data from a wide range of sources through APIs. Its intuitive design and extensive feature set make it an essential asset for any organization dealing with complex data integration tasks.
To get started with examples using ZappySys API Driver, please click on the following applications:
Key features of the ZappySys API Driver include:
The API ODBC driver facilitates the reading and writing of data from numerous popular online services (refer to the complete list here) using familiar SQL language without learning complexity of REST API calls. The driver allows querying nested structure and output as a flat table. You can also create your own ODBC / Data Gateway API connector file and use it with this driver.
Intuitive Configuration: The interface is designed to be user-friendly, enabling users to easily set up the specific API endpoints within Cosmos DB without requiring extensive technical expertise or programming knowledge.
Customizable Endpoint Setup: Users can conveniently configure the API endpoint settings, including the HTTP request method, endpoint URL, and any necessary parameters, to precisely target the desired data within Cosmos DB.
Data Manipulation Capabilities: The ZappySys API Driver allows for seamless data retrieval and writing, enabling users to fetch data from Cosmos DB and perform various data manipulation operations as needed, all through an intuitive and straightforward interface.
Secure Authentication Integration: The driver provides secure authentication integration, allowing users to securely connect to the Cosmos DB API by inputting the necessary authentication credentials, such as API tokens or other authentication keys.
Error Handling Support: The interface is equipped with comprehensive error handling support, ensuring that any errors or exceptions encountered during the data retrieval or writing process are efficiently managed and appropriately communicated to users for prompt resolution.
Data Visualization and Reporting: The ZappySys API Driver facilitates the seamless processing and presentation of the retrieved data from Cosmos DB, enabling users to generate comprehensive reports and visualizations for further analysis and decision-making purposes.
Overall, the ZappySys API Driver serves as a powerful tool for streamlining the integration of applications with Cosmos DB, providing users with a convenient and efficient way to access and manage data, all through a user-friendly and intuitive interface.