Skip to main content

Cinchy DML statements

Overview

Use Data Manipulation Language (DML) to add, retrieve, update and manipulate data. The Cinchy DML statements covered on this page are:

SELECT

Use the SELECT statement to select data from a database. The data returned is stored in a result table, called the result set.

Syntax

SELECT [Column1],[Column2], ...
FROM [Domain].[Table_Name]

Example

The below example will return the Full Name, Email, and Start Date columns from the [Employee] table in the [HR] domain.

SELECT [Full Name], [Email Address], [Start Date]
FROM [HR].[Employee]

Produce a nested JSON

You can create a query that will produce a nested JSON by wrapping it in an outer SELECT statement, such as in the example below, which uses FOR JSON PATH

caution

Note that you should set the return type to Single Value (First Column of First Row).

Example

SELECT (
SELECT
(SELECT p1.[Parent A],p1.[Parent B]
FROM [QA].[Parents 5080] p1 WHERE p1.[Parents]=p.[Parents]
FOR JSON PATH) As Parents,
(SELECT c.[Name] AS [Child Name]
FROM [QA].[Children 5080] c
WHERE c.[Deleted] IS NULL AND c.[Parents]= p.[Parents]
FOR JSON PATH) AS Children
FROM [QA].[Parents 5080] p
WHERE p.[Deleted] IS NULL
FOR JSON PATH, INCLUDE_NULL_VALUES)

Output

[
{
"Parents": [{ "Parent A": "Tom", "Parent B": "Maria" }],
"Children": [
{ "Child Name": "John" },
{ "Child Name": "Theodor" },
{ "Child Name": "Lynette" }
]
},
{
"Parents": [{ "Parent A": "Ray", "Parent B": "Sofia" }],
"Children": [{ "Child Name": "Stephen" }]
},
{
"Parents": [{ "Parent A": "Greg", "Parent B": "Mona" }],
"Children": [{ "Child Name": "Elizabeth" }, { "Child Name": "Martin" }]
},
{
"Parents": [{ "Parent A": "Henry", "Parent B": "Susanne" }],
"Children": null
}
]

INSERT

Use an INSERT statement to add new rows to a table or view. You can also include a SELECT statement to identify that another table or view contains the data for the new row or rows.

INSERT INTO [Domain].[Table_Name] ([Column1],[Column2],[Column3], ...)
VALUES ([Value1],[Value2],[Value3], ...)

Example

The below example allows you to insert First Name, Last Name, Address, and Job Title variables into their respective columns within the [People] table.

INSERT INTO [Contacts].[People] ([First Name],[Last Name],[Address],[Job Title])
VALUES (@firstname, @lastname, @address, @jobtitle)

UPDATE

Use the UPDATE statement to change the data in a table. The UPDATE statement modifies zero or more rows of a table, depending on how many rows meet the search condition specified in the WHERE clause. You can also use an UPDATE statement to specify the values to be updated in a single row. To do this, specify the constants, host variables, expressions, DEFAULT, or NULL. Specify NULL to remove a value from a row's column (without removing the row).

Syntax

UPDATE [Domain].[Table_Name]
SET [Column1] = [Value1], [Column2] = [value2], ...
WHERE [condition]

Example

The below example updates the [Customers] table such that the Contact Name and City are updated within the row with the matching Customer ID.

UPDATE [Revenue].[Customers]
SET [ContactName] = 'Alfred Schmidt', [City]= 'Frankfurt'
WHERE [Customer_ID] = 1;

DELETE

Use the DELETE statement to remove entire rows from a table. The number of rows deleted depends on how many rows match the search condition specified in the WHERE statement.

Syntax

DELETE FROM [Domain].[Table_Name] WHERE [condition]

Example

The below example deletes any row(s) in the [Customers] table where the Customer Name column is "Alfreds Futterkiste".

DELETE FROM [Revenue].[Customers] WHERE [CustomerName] = 'Alfreds Futterkiste';

IF

Use the IF statement to execute a condition. If the condition is satisfied, then the Boolean expressions returns TRUE value. The optional ELSE keyword introduces another statement that executes when the IF condition isn't satisfied and returns FALSE value.

Syntax

IF [condition] THEN [value_if_true] ELSE [value_if_false]

Example

IF 500<1000 THEN 'YES' ELSE 'NO'

DECLARE

Use the DECLARE statement to declare a variable.

Syntax

DECLARE @variable_name variable_type;

Example

DECLARE @var varchar(50);

SET

Use the SET variable to assign a value to a variable.

Syntax

SET @variable_name = variable_value

Example

SET @var = 'Alfreds Futterkiste'