SnowConvert AI - SQL Server-Azure Synapse - SELECT¶
SELECT¶
Translation reference for SELECT statement inside procedures in Transact-SQL.
Applies to
- SQL Server
- Azure Synapse Analytics
Note
Multiple result sets are returned in temporary tables
Description¶
Snowflake SQL supports returning tables as a return type for Stored Procedures, but unlike Transact-SQL, Snowflake does not support returning multiple resultsets in the same procedure. For this scenario, all the query IDs are stored in a temporary table and returned as an array.
Sample Source Patterns¶
The following example details the transformation when there is only one SELECT statement in the procedure.
Transact-SQL¶
Single Resultset¶
Output¶
| DepartmentID | Name | GroupName |
|---|---|---|
| 1 | Engineering | Research and Development |
| 2 | Tool Design | Research and Development |
| 3 | Sales | Sales and Marketing |
| 4 | Marketing | Sales and Marketing |
| 5 | Purchasing | Inventory Management |
| 6 | Research and Development | Research and Development |
| 7 | Production | Manufacturing |
| 8 | Production Control | Manufacturing |
| 9 | Human Resources | Executive General and Administration |
| 10 | Finance | Executive General and Administration |
| 11 | Information Services | Executive General and Administration |
| 12 | Document Control | Quality Assurance |
| 13 | Quality Assurance | Quality Assurance |
| 14 | Facilities and Maintenance | Executive General and Administration |
| 15 | Shipping and Receiving | Inventory Management |
| 16 | Executive | Executive General and Administration |
Snowflake SQL¶
Single Resultset¶
Output¶
| DepartmentID | Name | GroupName |
|---|---|---|
| 1 | Engineering | Research and Development |
| 2 | Tool Design | Research and Development |
| 3 | Sales | Sales and Marketing |
| 4 | Marketing | Sales and Marketing |
| 5 | Purchasing | Inventory Management |
| 6 | Research and Development | Research and Development |
| 7 | Production | Manufacturing |
| 8 | Production Control | Manufacturing |
| 9 | Human Resources | Executive General and Administration |
| 10 | Finance | Executive General and Administration |
| 11 | Information Services | Executive General and Administration |
| 12 | Document Control | Quality Assurance |
| 13 | Quality Assurance | Quality Assurance |
| 14 | Facilities and Maintenance | Executive General and Administration |
| 15 | Shipping and Receiving | Inventory Management |
| 16 | Executive | Executive General and Administration |
The following example details the transformation when there are many SELECT statements in the procedure.
Transact-SQL¶
Multiple Resultset¶
Output¶
| DepartmentID | Name | GroupName |
|---|---|---|
| 1 | Engineering | Research and Development |
| 2 | Tool Design | Research and Development |
| 3 | Sales | Sales and Marketing |
| 4 | Marketing | Sales and Marketing |
| 5 | Purchasing | Inventory Management |
| 6 | Research and Development | Research and Development |
| 7 | Production | Manufacturing |
| 8 | Production Control | Manufacturing |
| 9 | Human Resources | Executive General and Administration |
| 10 | Finance | Executive General and Administration |
| 11 | Information Services | Executive General and Administration |
| 12 | Document Control | Quality Assurance |
| 13 | Quality Assurance | Quality Assurance |
| 14 | Facilities and Maintenance | Executive General and Administration |
| 15 | Shipping and Receiving | Inventory Management |
| 16 | Executive | Executive General and Administration |
| ShiftID | Name | StartTime | EndTime | ModifiedDate |
|---|---|---|---|---|
| 1 | Day | 07:00:00 | 15:00:00 | 2008-04-30 00:00:00.000 |
| 2 | Evening | 15:00:00 | 23:00:00 | 2008-04-30 00:00:00.000 |
| 3 | Night | 23:00:00 | 07:00:00 | 2008-04-30 00:00:00.000 |
Snowflake SQL¶
Single Resultset¶
Output¶
| DepartmentID | Name | GroupName |
|---|---|---|
| 1 | Engineering | Research and Development |
| 2 | Tool Design | Research and Development |
| 3 | Sales | Sales and Marketing |
| 4 | Marketing | Sales and Marketing |
| 5 | Purchasing | Inventory Management |
| 6 | Research and Development | Research and Development |
| 7 | Production | Manufacturing |
| 8 | Production Control | Manufacturing |
| 9 | Human Resources | Executive General and Administration |
| 10 | Finance | Executive General and Administration |
| 11 | Information Services | Executive General and Administration |
| 12 | Document Control | Quality Assurance |
| 13 | Quality Assurance | Quality Assurance |
| 14 | Facilities and Maintenance | Executive General and Administration |
| 15 | Shipping and Receiving | Inventory Management |
| 16 | Executive | Executive General and Administration |
| ShiftID | Name | StartTime | EndTime | ModifiedDate |
|---|---|---|---|---|
| 1 | Day | 07:00:00 | 15:00:00 | 2008-04-30 00:00:00.000 |
| 2 | Evening | 15:00:00 | 23:00:00 | 2008-04-30 00:00:00.000 |
| 3 | Night | 23:00:00 | 07:00:00 | 2008-04-30 00:00:00.000 |
Known Issues¶
- The query results should be accessed by using the IDs returned by the Stored Procedure
Related EWIs¶
- SSC-FDM-0020: Multiple result sets are returned in temporary tables.
TOP¶
Applies to
- SQL Server
- Azure Synapse Analytics
Description¶
Note
Some parts in the output code are omitted for clarity reasons.
Limits the rows returned in a query result set to a specified number of rows or percentage of rows. When you use TOP with the ORDER BY clause, the result set is limited to the first N number of ordered rows. Otherwise, TOP returns the first N number of rows in an undefined order. Use this clause to specify the number of rows returned from a SELECT statement. Or, use TOP to specify the rows affected by an INSERT, UPDATE, MERGE, or DELETE statement. (Transact-SQL TOP documentation (https://learn.microsoft.com/en-us/sql/t-sql/queries/top-transact-sql?view=sql-server-ver16))
Syntax in Transact-SQL¶
Note
To get more information about the TOP arguments please check the Transact-SQL TOP documentation (https://learn.microsoft.com/en-us/sql/t-sql/queries/top-transact-sql?view=sql-server-ver16#arguments).
Syntax in Snowflake¶
Note
To get more information about TOP arguments please check the Snowflake TOP documentation.
Sample Source Patterns¶
To execute correctly the following samples it is required run the next CREATE TABLE statement:
Transact-SQL¶
Snowflake¶
Common Case¶
Transact-SQL¶
Query¶
Result¶
| Model | Color | Price |
|---|---|---|
| sedan | red | 10000.0000 |
Snowflake¶
Query¶
Result¶
| MODEL | COLOR | PRICE |
|---|---|---|
| sedan | red | 10,000 |
TOP using PERCENT¶
Transact-SQL¶
Query¶
Result¶
| Model | Color | Prices |
|---|---|---|
| sedan | red | 10000.0000 |
| convertible | blue | 15000.0000 |
| coupe | green | 20000.0000 |
Snowflake¶
Query¶
Result¶
| MODEL | COLOR | PRICE |
|---|---|---|
| sedan | red | 10,000 |
| convertible | blue | 15,000 |
| coupe | red | 20,000 |
| van | blue | 8,000 |
| sub | green | 8,000 |
Warning
Since PERCENT argument is not supported by Snowflake it is being removed from the TOP clause, that’s why the result of executing the query in Snowflake is not equivalent to Transact-SQL.
TOP WITH TIES¶
Transact-SQL¶
Query¶
Result¶
| Model | Color | Price |
|---|---|---|
| van | blue | 8000.0000 |
| sub | green | 8000.0000 |
| sedan | red | 10000.0000 |
Snowflake¶
Query¶
Result¶
| MODEL | COLOR | PRICE |
|---|---|---|
| sub | green | 8,000 |
| van | blue | 8,000 |
| sedan | red | 10,000 |
| convertible | blue | 15,000 |
| coupe | red | 20,000 |
Warning
Since WITH TIES argument is not supported by Snowflake it is being removed from the TOP clause, that’s why the result of executing the query in Snowflake is not equivalent to Transact-SQL.
Known Issues¶
1. PERCENT argument is not supported by Snowflake¶
Since the PERCENT argument is not supported by Snowflake it is being removed from the TOP clause and a warning is being added. Functional equivalence mismatches in the results could happen.
2. WITH TIES argument is not supported by Snowflake¶
Since the WITH TIES argument is not supported by Snowflake it is being removed from the TOP clause and a warning is being added. Functional equivalence mismatches in the results could happen.
Related EWIs¶
- SSC-EWI-0040: Statement Not Supported.
Window-function ORDER BY NULL Ordering¶
Applies to
- SQL Server
- Azure Synapse Analytics
Description¶
The T-SQL family treats NULL as the smallest value, so ORDER BY x ASC sorts NULLs first and ORDER BY x DESC sorts NULLs last. Snowflake’s default treats NULL as the largest value, which silently inverts the position of NULLs and changes window-function ranks/values for any partition that contains NULLs in the order key.
To preserve T-SQL family semantics, SnowConvert AI emits an explicit NULLS FIRST or NULLS LAST clause on every ORDER BY item inside an OVER (...) window-function clause that does not already carry one:
| Source direction | Emitted clause |
|---|---|
ASC (or no direction — ASC is the parser default) | NULLS FIRST |
DESC | NULLS LAST |
The rewrite is idempotent: items that already specify a NULLS … clause are preserved verbatim, including caller overrides such as ORDER BY x ASC NULLS LAST.
Sample Source Patterns¶
Input Code:¶
Transact-SQL¶
Output Code:¶
Snowflake¶
Known Issues¶
- Scope is limited to window-function
OVER (...)clauses. Statement-levelORDER BY(outsideOVER) andWITHIN GROUP (ORDER BY ...)carry the same NULL-ordering divergence but are intentionally not rewritten. If the result of aSELECT ... ORDER BY xquery depends on NULL position, add an explicitNULLS FIRST/NULLS LASTclause manually or set the session parameterDEFAULT_NULL_ORDERING. - Two unsupported functions do not receive the rewrite:
EXP_WEIGHTED_AVGandWEIGHTED_AVG. Their EWI-flagging replacer wraps the function before the innerORDER BYis visited, so the NULLS clause is not added. Both already require manual rework by the user (they emitSSC-EWI-0073), so the missing clause is moot in practice.