SnowConvert AI - SQL Server-Azure Synapse - DMLs¶
BETWEEN¶
Returns TRUE when the input expression (numeric or string) is within the specified lower and upper boundary.
Applies to
SQL Server
Azure Synapse Analytics
Note
Some parts in the output code are omitted for clarity reasons.
Source Code
Code Expected
BULK INSERT¶
Translation reference for the Bulk Insert statement.
Applies to
SQL Server
Azure Synapse Analytics
Some parts in the output code are omitted for clarity reasons.
The direct translation for BULK INSERT is the Snowflake COPY INTO statement. The COPY INTO does not use directly the file path to retrieve the values. The file should exist before in a STAGE. Also the options used in the BULK INSERT should be specified in a Snowflake FILE FORMAT that will be consumed by the STAGE or directly by the COPY INTO.
To add a file to some STAGE you should use the PUT command. Notice that the command can be executed only from the SnowSQL CLI. Here is an example of the steps we should do before executing a COPY INTO:
SQL Server¶
Snowflake¶
As you see in the code above, SnowConvert AI identifies all the BULK INSERTS in the code, and for each instance, a new STAGE and FILE FORMAT will be created before the copy into execution. In addition, after the creation of the STAGE, a PUT command will be created as well to add the file to the stage.
The names of the generated statements are auto-generated using the current timestamp in seconds, to avoid collisions between their usages.
Finally, all the options for the bulk insert are being mapped to file format options if apply. If the option is not supported in Snowflake, it will be commented and a warning will be added. See also SSC-FDM-TS0004.
Supported bulk options¶
SQL Server |
Snowflake |
|---|---|
FORMAT |
TYPE |
FIELDTERMINATOR |
FIELD_DELIMITER |
FIRSTROW |
SKIP_HEADER |
ROWTERMINATOR |
RECORD_DELIMITER |
FIELDQUOTE |
FIELD_OPTIONALLY_ENCLOSED_BY |
Common Table Expression (CTE)¶
Applies to
SQL Server
Azure Synapse Analytics
Some parts in the output code are omitted for clarity reasons.
Common table expressions are supported in Snowflake SQL by default.
Syntax¶
Snowflake SQL¶
Subquery:
Recursive CTE:
Where:
Noteworthy details¶
The RECURSIVE keyword does not exist in T-SQL, and the transformation does not actively add the keyword to the result. A warning is added to the output code to state this behavior.
Common Table Expression with SELECT INTO¶
The following transformation occurs when the WITH expression is followed by an SELECT INTO statement and it will be transformed into a TEMPORARY TABLE.
SQL Server:¶
Snowflake:¶
Common Table Expression with other expressions¶
The following transformation occurs when the WITH expression is followed by INSERT or DELETE statements.
SQL Server:¶
Snowflake:¶
Common Table Expression with Delete From¶
For this transformation, it will only apply for a CTE (Common Table Expression) with a Delete From, however, only for some specifics CTE. It must have only one CTE, and it must have inside a function of ROW_NUMBER or RANK.
The purpose of the CTE with the Delete must be to remove duplicates from a table. In case that the CTE with Delete intents to remove another kind of data, this transformation will not apply.
Let’s see an example. For a working example, we must first create a table with some data.
Note that there is a duplicated value. The lines 8 and 12 insert the same value. Now we are going to eliminate the duplicates rows in a table.
If we execute a Select from the table, it will show the following result
ID |
Value |
StringValue |
|---|---|---|
100 |
100 |
First |
200 |
200 |
Second |
300 |
300 |
Third |
400 |
400 |
Fourth |
Note that the duplicated rows have been removed. To preserve this functionality in Snowflake, which does not support DELETE from a CTE, SnowConvert transforms the statement into the following:
As you can see, the query is transformed to a Create Or Replace Table.
To test it in Snowflake, you will need the table.
Now, if we execute the result of the transformation, and then a Select to check if the duplicated rows were deleted, this would be the result.
ID |
Value |
StringValue |
|---|---|---|
100 |
100 |
First |
200 |
200 |
Second |
300 |
300 |
Third |
400 |
400 |
Fourth |
Common Table Expression with MERGE statement¶
The following transformation occurs when the WITH expression is followed by MERGE statement and it will be transformed into a MERGE INTO.
SQL Server:¶
Snowflake:¶
Common Table Expression with UPDATE statement¶
The following transformation occurs when the WITH expression is followed by an UPDATE statement and it will be transformed into an UPDATE statement.
SQL Server:¶
Snowflake:¶
Known Issues¶
No issues were found.
Related EWIs¶
SSC-EWI-0108: The following subquery matches at least one of the patterns considered invalid and may produce compilation errors.
SSC-PRF-TS0001: Performance warning - recursion for CTE not checked. Might require a recursive keyword.
DELETE¶
Translation reference for Transact-SQL Delete statement to Snowflake
Applies to
SQL Server
Azure Synapse Analytics
Note
Some parts in the output code are omitted for clarity reasons.
Description¶
Removes one or more rows from a table or view in SQL Server. For more information, see the SQL Server DELETE documentation.
Sample Source Patterns¶
Sample Data¶
SQL Server¶
Snowflake¶
Basic Case¶
The transformation for the DELETE statement is fairly straightforward, with some caveats. One of these caveats is the way Snowflake supports multiple sources in the FROM clause, however, there is an equivalent in Snowflake as shown below.
SQL Server¶
Snowflake¶
Note
Note that, since the original DELETE was for T1, the presence of TABLE2 T2 in the FROM clause requires the creation of the USING clause.
Delete duplicates from a table¶
The following documentation explains a common pattern used to remove duplicated rows from a table in SQL Server. This approach uses the ROW_NUMBER function to partition the data based on the key_value which may be one or more columns separated by commas. Then, delete all records that received a row number value that is greater than 1. This value indicates that the records are duplicates. You can read the referenced documentation to understand the behavior of this method and recreate it.
The following example uses this approach to remove duplicates from a table and its equivalent in Snowflake. The transformation consists of performing an INSERT OVERWRITE statement which truncates the table (removes all data) and then inserts again the rows in the same table ignoring the duplicated ones. The output code is generated considering the same PARTITION BY and ORDER BY clauses used in the original code.
SQL Server¶
Create a table with duplicated rows
Insert duplicates¶
Output¶
someID |
col2 |
col3 |
col4 |
col5 |
|---|---|---|---|---|
10 |
true |
false |
false |
true |
10 |
true |
false |
false |
true |
11 |
true |
true |
false |
true |
12 |
false |
false |
true |
true |
12 |
false |
false |
true |
true |
13 |
true |
false |
true |
false |
14 |
true |
false |
true |
false |
14 |
true |
false |
true |
false |
Remove duplicates¶
Output¶
someID |
col2 |
col3 |
col4 |
col5 |
|---|---|---|---|---|
10 |
true |
false |
false |
true |
11 |
true |
true |
false |
true |
12 |
false |
false |
true |
true |
13 |
true |
false |
true |
false |
14 |
true |
false |
true |
false |
Snowflake¶
Create a table with duplicated rows
Insert duplicates¶
Output¶
someID |
col2 |
col3 |
col4 |
col5 |
|---|---|---|---|---|
10 |
true |
false |
false |
true |
10 |
true |
false |
false |
true |
11 |
true |
true |
false |
true |
12 |
false |
false |
true |
true |
12 |
false |
false |
true |
true |
13 |
true |
false |
true |
false |
14 |
true |
false |
true |
false |
14 |
true |
false |
true |
false |
Remove duplicates¶
Output¶
someID |
col2 |
col3 |
col4 |
col5 |
|---|---|---|---|---|
10 |
true |
false |
false |
true |
11 |
true |
true |
false |
true |
12 |
false |
false |
true |
true |
13 |
true |
false |
true |
false |
14 |
true |
false |
true |
false |
Warning
Consider that there may be several variations of this pattern, but all of them are based on the same principle and have the same structure.
DELETE WITH INNER JOIN¶
SQL SERVER¶
Output¶
EmployeeID |
FirstName |
LastName |
DepartmentID |
|---|---|---|---|
5 |
Michael |
Davis |
null |
6 |
Lucas |
Parker |
8 |
Snowflake¶
Output¶
EmployeeID |
FirstName |
LastName |
DepartmentID |
|---|---|---|---|
5 |
Michael |
Davis |
null |
6 |
Lucas |
Parker |
8 |
DELETE WITH LEFT JOIN¶
SQL Server¶
Output¶
EmployeeID |
FirstName |
LastName |
DepartmentID |
|---|---|---|---|
1 |
John |
Doe |
1 |
2 |
Jane |
Smith |
2 |
3 |
Bob |
Johnson |
1 |
4 |
Alice |
Brown |
3 |
Snowflake¶
Output¶
EmployeeID |
FirstName |
LastName |
DepartmentID |
|---|---|---|---|
1 |
John |
Doe |
1 |
2 |
Jane |
Smith |
2 |
3 |
Bob |
Johnson |
1 |
4 |
Alice |
Brown |
3 |
DELETE WITH RIGHT JOIN¶
SQL SERVER¶
Output¶
EmployeeID |
FirstName |
LastName |
DepartmentID |
|---|---|---|---|
5 |
Michael |
Davis |
null |
6 |
Lucas |
Parker |
8 |
Snowflake¶
Output¶
EmployeeID |
FirstName |
LastName |
DepartmentID |
|---|---|---|---|
5 |
Michael |
Davis |
null |
6 |
Lucas |
Parker |
8 |
Known Issues¶
FULL JOIN not supported
The FULL JOIN can not be represented using the (+) syntax. When found, SnowConvert AI will warn the user about this with an FDM.
SQL Server¶
Snowflake¶
Related EWIs¶
SSC-EWI-TS0081: Using a full join in a delete statement is not supported
DROP STATEMENT¶
DROP statements
Applies to
SQL Server
Azure Synapse Analytics
Some parts in the output code are omitted for clarity reasons.
DROP TABLE¶
Transact-SQL¶
Snowflake¶
Translation¶
Translation for single DROP TABLE statements is very straightforward. As long as there is only one table being dropped within the statement, it’s left as-is.
For example:
The only noteworthy difference between SQL Server and Snowflake appears when the input statement drops more than one table. In these scenarios, a different DROP TABLE statement is created for each table being dropped.
For example:
SQL Server¶
Snowflake¶
EXISTS¶
Transact-SQL subqueries using EXISTS statement transformation details
Applies to
SQL Server
Azure Synapse Analytics
Some parts in the output code are omitted for clarity reasons.
Types of Subqueries¶
Subqueries can be categorized as correlated or uncorrelated:
A correlated subquery, refers to one or more columns from outside of the subquery. (The columns are typically referenced inside the WHERE clause of the subquery.) A correlated subquery can be thought of as a filter on the table that it refers to, as if the subquery were evaluated on each row of the table in the outer query.
An uncorrelated subquery, has no such external column references. It is an independent query, the results of which are returned to and used by the outer query once (not per row).
The EXISTS statement is considered a correlated subquery.
SQL SERVER¶
Snowflake¶
IN¶
Transact-SQL subqueries using IN statement transformation details
Applies to
SQL Server
Azure Synapse Analytics
Some parts in the output code are omitted for clarity reasons.
The IN operator checks if an expression is included in the values returned by a subquery.
SQL SERVER¶
Snowflake¶
INSERT¶
Translation reference for SQL Server Insert statement to Snowflake
Applies to
SQL Server
Azure Synapse Analytics
Some parts in the output code are omitted for clarity reasons.
Description¶
Adds one or more rows to a table or a view in SQL Server. For more information, see the SQL Server INSERT documentation.
Syntax comparison¶
The basic insert grammar is equivalent between both SQL languages. However there are still some other syntax elements in SQL Server that show differences, for example, one allows the developer to add a value to a column by using the assign operator. The syntax mentioned will be transformed to the basic insert syntax too.
Snowflake¶
SQL Server¶
Sample Source Patterns¶
Basic INSERT¶
SQL Server¶
Snowflake¶
INSERT with assign operator¶
SQL Server¶
Snowflake¶
INSERT with no INTO¶
SQL Server¶
Snowflake¶
INSERT with common table expression¶
SQL Server¶
Snowflake¶
INSERT with Table DML Factor with MERGE as DML¶
This case is so specific where the INSERT statement has a SELECT query, and the FROM clause of the SELECT mentioned contains a MERGE DML statement.
Looking for an equivalent in Snowflake, the next statements are created: a temporary table, the merge statement converted, and finally, the insert statement.
SQL Server¶
Snowflake¶
NOTE: As the pattern’s name suggests, it is ONLY for cases where the insert comes with a select…from which the body contains a MERGE statement.
Known Issues¶
1. Syntax elements that require special mappings:
[INTO]: This keyword is obligatory in Snowflake and should be added if not present.
[DEFAULT VALUES]: Inserts the default value in all columns specified in the insert. Should be transformed to VALUES (DEFAULT, DEFAULT, …), the amount of DEFAULTs added equals the number of columns the insert will modify. For now, there is a warning being added.
SQL Server¶
Snowflake¶
2. Syntax elements not supported or irrelevant:
[TOP (expression) [PERCENT]]: Indicates the amount or percent of rows that will be inserted. Not supported.
[rowset_function_limited]: It is either OPENQUERY() or OPENROWSET(), used to read data from remote servers. Not supported.
[WITH table_hint_limited]: These are used to get reading/writing locks on tables. Not relevant in Snowflake.
[<OUTPUT Clause>]: Specifies a table or result set in which the inserted rows will also be inserted. Not supported.
[execute_statement]: Can be used to run a query to get data from. Not supported.
[dml_table_source]: A temporary result set generated by the OUTPUT clause of another DML statement. Not supported.
3. The DELETE case is not being considered.
For the INSERT with Table DML Factor with MERGE as DML pattern, the DELETE case is not being considered in the solution, so if the source code merge statement has a DELETE case please consider that it might not work as expected.
Related EWIs¶
SSC-EWI-0073: Pending Functional Equivalence Review.
SSC-FDM-TS0026: DELETE case is not being considered.
MERGE¶
Transact-SQL MERGE statement transformation details
Applies to
SQL Server
Azure Synapse Analytics
Syntax comparison¶
Snowflake¶
Transact-SQL¶
Example¶
Given the following source code:
SQL Server¶
You can expect to get something like this:
Snowflake¶
Related EWIs¶
SSC-EWI-0021: Syntax not supported in Snowflake.
SELECT¶
Translation reference to convert SQL Server Select statement to Snowflake
Applies to
SQL Server
Azure Synapse Analytics
Some parts in the output code are omitted for clarity reasons.
Description¶
Allows the selection of one or more rows or columns of one or more tables in SQL Server. For more information, see the SQL Server SELECT documentation.
Sample Source Patterns¶
SELECT WITH COLUMN ALIASES¶
The following example demonstrates how to use column aliases in Snowflake. The first two columns, from the SQL Server code, are expected to be transformed from an assignment form into a normalized form using the AS keyword. The third and fourth columns are using valid Snowflake formats.
SQL Server¶
Snowflake¶
SELECT TOP¶
SQL Server¶
Snowflake¶
SELECT INTO¶
The following example shows the SELECT INTO is transformed into a CREATE TABLE AS, this is because in Snowflake there is no equivalent for
SELECT INTO and to create a table based on a query has to be with the CREATE TABLE AS.
SQL Server¶
Snowflake¶
Another case is when including set operators such as EXCEPT and INTERSECT. The transformation is basically the same as the previous one.
SQL Server¶
Snowflake¶
SELECT TOP Additional Arguments¶
Since PERCENT and WITH TIES keywords affect the result, and they are not supported by Snowflake, they will be commented out and added as an error.
SQL Server¶
Snowflake¶
SELECT FOR¶
Since the FOR clause is not supported in Snowflake, it is commented out and added as an error during the transformation.
SQL Server¶
Snowflake¶
SELECT OPTION¶
The OPTION clause is not supported by Snowflake. It will be commented out and added as a warning during the transformation.
Notice that the OPTION statement has been removed from transformation because it is not relevant or not needed in Snowflake.
SQL Server¶
Snowflake¶
SELECT WITH¶
The WITH clause is not supported by Snowflake. It will be commented out and added as a warning during the transformation.
Notice that the WITH(NOLOCK, NOWAIT) statement has been removed from transformation because it is not relevant or not needed in Snowflake.
SQL Server¶
Snowflake¶
Related EWIs¶
SSC-EWI-0040: Statement Not Supported.
SSC-FDM-TS0016: XML columns in Snowflake might have a different format
SET OPERATORS¶
Applies to
SQL Server
Azure Synapse Analytics
Set Operators in both TSQL and Snowflake present the same syntax and supported scenarios(EXCEPT, INTERSECT, UNION and UNION ALL), with the exception of the MINUS which is not supported in TSQL, resulting in the same code during conversion.
TRUNCATE¶
Transact-SQL TRUNCATE statement transformation details
Applies to
SQL Server
Azure Synapse Analytics
Some parts in the output code are omitted for clarity reasons.
SQL Server¶
Snowflake¶
UPDATE¶
Translation reference to convert SQL Server Update statement to Snowflake
Applies to
SQL Server
Azure Synapse Analytics
Note
Some parts in the output code are omitted for clarity reasons.
Description¶
Changes existing data in a table or view in SQL Server. For more information, see the SQL Server UPDATE documentation.
Sample Source Patterns¶
Basic UPDATE¶
The conversion for a regular UPDATE statement is very straightforward. Since the basic UPDATE structure is supported by default in Snowflake, the outliers are the parts where you are going to see some differences.
SQL Server¶
Snowflake¶
Cartesian Products¶
SQL Server allows add circular references between the target table of the Update Statement and the FROM Clause/ In execution time, the database optimizer removes any cartesian product generated. Otherwise, Snowflake currently does not optimize this scenario, producing a cartesian product that can be checked in the Execution Plan.\
To resolve this, if there is a JOIN where one of their tables is the same as the update target, this reference is removed and added to the WHERE clause, and it is used to just filter the data and avoid making a set operation.
SQL Server¶
Snowflake¶
OUTPUT clause¶
The OUTPUT clause is not supported by Snowflake.
SQL Server¶
Snowflake¶
CTE¶
The WITH CTE clause is moved to the internal query in the update statement to be supported by Snowflake.
SQL Server¶
Snowflake¶
TOP clause¶
The TOP clause is not supported by Snowflake.
SQL Server¶
Snowflake¶
WITH TABLE HINT LIMITED¶
The Update WITH clause in not supported by Snowflake.
SQL Server¶
Snowflake¶
Related EWIs¶
SSC-EWI-0021: Syntax not supported in Snowflake.
UPDATE WITH JOIN¶
Translation specification for UPDATE statement with WHERE and JOIN clauses
Warning
This is a work in progress and may change in the future.
Description¶
The pattern UPDATE FROM is used to update data based on data from other tables. This SQLServer documentation provides a simple sample.
Review the following SQL Server syntax from the documentation.
SQL Server Syntax¶
table_name: The table or view you are updating.SET: Specifies the columns and their new values. TheSETclause assigns a new value (or expression) to one or more columns.FROM: Used to specify one or more source tables (like a join). It helps define where the data comes from to perform the update.WHERE: Specifies which rows should be updated based on the condition(s). Without this clause, all rows in the table would be updated.OPTION (query_hint): Specifies hints for query optimization.
Snowflake syntax¶
The Snowflake syntax can also be reviewed in the Snowflake documentation.
Note
Snowflake does not support JOINs in UPDATE clause.
Required parameters
target_table:Specifies the table to update.col_name:Specifies the name of a column intarget_table. Do not include the table name. E.g.,UPDATE t1 SET t1.col = 1is invalid.value:Specifies the new value to set incol_name.
Optional parameters
FROM``additional_tables:Specifies one or more tables to use for selecting rows to update or for setting new values. Note that repeating the target table results in a self-join.WHERE``condition:The expression that specifies the rows in the target table to update. Default: No value (all rows of the target table are updated)
Translation Summary¶
SQL Server JOIN type |
Snowflake Best Alternative |
|---|---|
Single |
Use the target table in the |
Multiple |
Use the target table in the |
Multiple |
Use subquery + IN Operation |
Single |
Use subquery + IN Operation |
Multiple |
Use Snowflake |
Multiple |
Use Snowflake |
Single RIGHT JOIN |
Use the table in the |
Note-1: Simple JOIN may use the table in the FROM clause and add filters in the WHERE clause as needed.
Note-2: Other approaches may include (+) operand to define the JOINs.
Sample Source Patterns¶
Setup data¶
SQLServer¶
Snowflake¶
Data Insertion for samples
Case 1: Single INNER JOIN Update¶
For INNER JOIN, if the table is used inside the FROM statements, it automatically turns into INNER JOIN. Notice that there are several approaches to support JOINs in UPDATE statements in Snowflake. This is one of the simplest patterns to ensure readability.
SQL Server¶
Output¶
CustomerID |
Quantity |
CustomerName |
|---|---|---|
1 |
10 |
John Doe |
Snowflake¶
Output¶
CustomerID |
Quantity |
CustomerName |
|---|---|---|
1 |
10 |
John Doe |
Other approaches:
MERGE INTO
IN Operation
Case 2: Multiple INNER JOIN Update¶
SQL Server¶
Output¶
CustomerID |
Quantity |
CustomerName |
|---|---|---|
3 |
5 |
Alice Johnson |
Snowflake¶
Output¶
CustomerID |
Quantity |
CustomerName |
|---|---|---|
3 |
5 |
Alice Johnson |
Case 3: Multiple INNER JOIN Update with Aggregate Condition¶
SQL Server¶
Output¶
CustomerID |
CustomerName |
Quantity |
Price |
|---|---|---|---|
11 |
Jack Grey |
6 |
29.99 |
18 |
Quincy Brown |
6 |
15.99 |
20 |
Sam Green |
6 |
89.99 |
22 |
Ursula Red |
6 |
9.99 |
24 |
Wendy Black |
6 |
49.99 |
Snowflake¶
Output¶
CustomerID |
CustomerName |
Quantity |
Price |
|---|---|---|---|
11 |
Jack Grey |
6 |
29.99 |
18 |
Quincy Brown |
6 |
15.99 |
20 |
Sam Green |
6 |
89.99 |
22 |
Ursula Red |
6 |
9.99 |
24 |
Wendy Black |
6 |
49.99 |
Case 4: Single LEFT JOIN Update¶
SQL Server¶
Output¶
OrderID |
CustomerID |
ProductID |
Quantity |
OrderDate |
|---|---|---|---|---|
5 |
null |
5 |
7 |
2024-11-05 |
13 |
null |
13 |
13 |
2024-11-13 |
Snowflake¶
Output¶
OrderID |
CustomerID |
ProductID |
Quantity |
OrderDate |
|---|---|---|---|---|
5 |
null |
5 |
7 |
2024-11-05 |
13 |
null |
13 |
13 |
2024-11-13 |
Note
This approach in Snowflake will not work because it does not update the necessary rows:
UPDATE Orders O SET O.Quantity = 13 FROM Customers C WHERE O.CustomerID = C.CustomerID AND C.CustomerID IS NULL AND O.ProductID = 13;
Case 5: Multiple LEFT JOIN and RIGHT JOIN Update¶
This is a more complex pattern. To translate multiple LEFT JOINs, please review the following pattern:
Note
LEFT JOIN and RIGHT JOIN will depend on the order in the FROM clause.
SQL Server¶
Output¶
OrderID |
CustomerID |
ProductID |
Quantity |
OrderDate |
|---|---|---|---|---|
3 |
3 |
3 |
3 |
2024-11-12 |
Snowflake¶
Output¶
OrderID |
CustomerID |
ProductID |
Quantity |
OrderDate |
|---|---|---|---|---|
3 |
3 |
3 |
3 |
2024-11-12 |
Case 6: Mixed INNER JOIN and LEFT JOIN Update¶
SQL Server¶
Output¶
CustomerID |
CustomerName |
Quantity |
|---|---|---|
null |
null |
4 |
Snowflake¶
Output¶
CustomerID |
CustomerName |
Quantity |
|---|---|---|
null |
null |
4 |
Case 7: Single RIGHT JOIN Update¶
SQL Server¶
Output¶
OrderID |
CustomerID |
ProductID |
Quantity |
CustomerName |
|---|---|---|---|---|
3 |
3 |
3 |
1000 |
Alice Johnson |
Snowflake¶
Output¶
OrderID |
CustomerID |
ProductID |
Quantity |
CustomerName |
|---|---|---|---|---|
3 |
3 |
3 |
1000 |
Alice Johnson |
Known Issues¶
Since
UPDATEin Snowflake does not allow the usage ofJOINsdirectly, there may be cases that do not match the patterns described.
UPDATE with LEFT and RIGHT JOIN¶
Translation specification for the UPDATE statement with JOINs.
Applies to
SQL Server
Azure Synapse Analytics
Warning
Partially supported in Snowflake
Description¶
The pattern UPDATE FROM is used to update data based on data from other tables. This SQLServer documentation provides a simple sample.
Review the following SQL Server syntax from the documentation.
SQL Server Syntax¶
table_name: The table or view you are updating.SET: Specifies the columns and their new values. TheSETclause assigns a new value (or expression) to one or more columns.FROM: Used to specify one or more source tables (like a join). It helps define where the data comes from to perform the update.WHERE: Specifies which rows should be updated based on the condition(s). Without this clause, all rows in the table would be updated.OPTION (query_hint): Specifies hints for query optimization.
Snowflake syntax¶
The Snowflake syntax can also be reviewed in the Snowflake documentation.
Note
Snowflake does not support JOINs in UPDATE clause.
Required parameters
target_table:Specifies the table to update.col_name:Specifies the name of a column intarget_table. Do not include the table name. E.g.,UPDATE t1 SET t1.col = 1is invalid.value:Specifies the new value to set incol_name.
Optional parameters
FROM``additional_tables:Specifies one or more tables to use for selecting rows to update or for setting new values. Note that repeating the target table results in a self-join.WHERE``condition:The expression that specifies the rows in the target table to update. Default: No value (all rows of the target table are updated)
Translation Summary¶
As it is explained in the grammar description, there is not straight forward equivalent solution for JOINs inside the UPDATE cluase. For this reason, the approach to transform this statements is to add the operator (+) on the column that logically will add the required data into the table. This operator (+) is added to the cases on which the tables are referenced in the LEFT/RIGHT JOIN section.
Notice that there are other languages that use this operator (+) and the position of the operator may determine the type of join. In this specific case in Snowflake, the position will not determine the join type but the association with the logically needed tables and columns will.
Even when there are other alternative as MERGE clause or the usages of a CTE; these alternatives tend to turn difficult to read when there are complex queries, and get extensive.
Sample Source Patterns¶
Setup data¶
SQL Server¶
Snowflake¶
LEFT JOIN¶
SQL Server¶
Output Before Query¶
Col1 |
Col2 |
Col3 |
Col4 |
Col5 |
Col6 |
|---|---|---|---|---|---|
1 |
A1 |
B1 |
C1 |
null |
null |
2 |
A2 |
B2 |
C2 |
null |
null |
3 |
A3 |
B3 |
C3 |
null |
null |
Output After Query¶
Col1 |
Col2 |
Col3 |
Col4 |
Col5 |
Col6 |
|---|---|---|---|---|---|
1 |
A1 |
B1 |
C1 |
X1 |
Description1 |
2 |
A2 |
B2 |
C2 |
X2 |
Description2 |
3 |
A3 |
B3 |
C3 |
X3 |
Description3 |
Snowflake¶
Output Before Query¶
Col1 |
Col2 |
Col3 |
Col4 |
Col5 |
Col6 |
|---|---|---|---|---|---|
1 |
A1 |
B1 |
C1 |
null |
null |
2 |
A2 |
B2 |
C2 |
null |
null |
3 |
A3 |
B3 |
C3 |
null |
null |
Output After Query¶
Col1 |
Col2 |
Col3 |
Col4 |
Col5 |
Col6 |
|---|---|---|---|---|---|
1 |
A1 |
B1 |
C1 |
X1 |
Description1 |
2 |
A2 |
B2 |
C2 |
X2 |
Description2 |
3 |
A3 |
B3 |
C3 |
X3 |
Description3 |
RIGHT JOIN¶
SQL Server¶
Output Before Query¶
Col1 |
Col2 |
Col3 |
Col4 |
Col5 |
Col6 |
|---|---|---|---|---|---|
1 |
A1 |
B1 |
C1 |
null |
null |
2 |
A2 |
B2 |
C2 |
null |
null |
3 |
A3 |
B3 |
C3 |
null |
null |
Output After Query¶
Col1 |
Col2 |
Col3 |
Col4 |
Col5 |
Col6 |
|---|---|---|---|---|---|
1 |
A1 |
B1 |
C1 |
**X1 |
null |
2 |
A2 |
B2 |
C2 |
**X2 |
null |
3 |
A3 |
B3 |
C3 |
**X3 |
null |
Snowflake¶
Output Before Query¶
Col1 |
Col2 |
Col3 |
Col4 |
Col5 |
Col6 |
|---|---|---|---|---|---|
1 |
A1 |
B1 |
C1 |
null |
null |
2 |
A2 |
B2 |
C2 |
null |
null |
3 |
A3 |
B3 |
C3 |
null |
null |
Output After Query¶
Col1 |
Col2 |
Col3 |
Col4 |
Col5 |
Col6 |
|---|---|---|---|---|---|
1 |
A1 |
B1 |
C1 |
**X1 |
null |
2 |
A2 |
B2 |
C2 |
**X2 |
null |
3 |
A3 |
B3 |
C3 |
**X3 |
null |
Known Issues¶
There may be patterns that cannot be translated due to differences in logic.
If your query pattern applies, review non-deterministic rows: “When a FROM clause contains a JOIN between tables (e.g.
t1andt2), a target row int1may join against (i.e. match) more than one row in tablet2. When this occurs, the target row is called a multi-joined row. When updating a multi-joined row, the ERROR_ON_NONDETERMINISTIC_UPDATE session parameter controls the outcome of the update” (Snowflake documentation).