You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
description: Explanation and examples of the CREATE TABLE AS SELECT (CTAS) statement in Synapse SQL for developing solutions.
3
+
description: Explanation and examples of the CREATE TABLE AS SELECT (CTAS) statement in dedicated SQL pool (formerly SQL DW) for developing solutions.
4
4
author: joannapea
5
5
manager: craigg
6
6
ms.service: synapse-analytics
7
7
ms.topic: conceptual
8
8
ms.subservice: sql-dw
9
-
ms.date: 03/26/2019
9
+
ms.date: 06/09/2022
10
10
ms.author: joanpo
11
11
ms.reviewer: wiassaf
12
12
ms.custom: seoapril2019, azure-synapse
13
13
---
14
14
15
15
# CREATE TABLE AS SELECT (CTAS)
16
16
17
-
This article explains the CREATE TABLE AS SELECT (CTAS) T-SQL statement in Synapse SQL for developing solutions. The article also provides code examples.
17
+
This article explains the CREATE TABLE AS SELECT (CTAS) T-SQL statement in dedicated SQL pool (formerly SQL DW) for developing solutions. The article also provides code examples.
18
18
19
19
## CREATE TABLE AS SELECT
20
20
@@ -55,7 +55,7 @@ FROM [dbo].[FactInternetSales];
55
55
56
56
Perhaps one of the most common uses of CTAS is creating a copy of a table in order to change the DDL. Let's say you originally created your table as `ROUND_ROBIN`, and now want to change it to a table distributed on a column. CTAS is how you would change the distribution column. You can also use CTAS to change partitioning, indexing, or column types.
57
57
58
-
Let's say you created this table by using the default distribution type of `ROUND_ROBIN`, not specifying a distribution column in the `CREATE TABLE`.
58
+
Let's say you created this table by specifying HEAP and using the default distribution type of `ROUND_ROBIN`.
59
59
60
60
```sql
61
61
CREATETABLEFactInternetSales
@@ -82,7 +82,12 @@ CREATE TABLE FactInternetSales
82
82
TaxAmt moneyNOT NULL,
83
83
Freight moneyNOT NULL,
84
84
CarrierTrackingNumber nvarchar(25),
85
-
CustomerPONumber nvarchar(25));
85
+
CustomerPONumber nvarchar(25)
86
+
)
87
+
WITH(
88
+
HEAP,
89
+
DISTRIBUTION = ROUND_ROBIN
90
+
);
86
91
```
87
92
88
93
Now you want to create a new copy of this table, with a `Clustered Columnstore Index`, so you can take advantage of the performance of Clustered Columnstore tables. You also want to distribute this table on `ProductKey`, because you're anticipating joins on this column and want to avoid data movement during joins on `ProductKey`. Lastly, you also want to add partitioning on `OrderDateKey`, so you can quickly delete old data by dropping old partitions. Here is the CTAS statement, which copies your old table into a new table.
@@ -115,116 +120,6 @@ RENAME OBJECT FactInternetSales_new TO FactInternetSales;
115
120
DROPTABLE FactInternetSales_old;
116
121
```
117
122
118
-
## Use CTAS to work around unsupported features
119
-
120
-
You can also use CTAS to work around a number of the unsupported features listed below. This method can often prove helpful, because not only will your code be compliant, but it will often run faster on Synapse SQL. This performance is a result of its fully parallelized design. Scenarios include:
121
-
122
-
* ANSI JOINS on UPDATEs
123
-
* ANSI JOINs on DELETEs
124
-
* MERGE statement
125
-
126
-
> [!TIP]
127
-
> Try to think "CTAS first." Solving a problem by using CTAS is generally a good approach, even if you're writing more data as a result.
128
-
129
-
## ANSI join replacement for update statements
130
-
131
-
You might find that you have a complex update. The update joins more than two tables together by using ANSI join syntax to perform the UPDATE or DELETE.
132
-
133
-
Imagine you had to update this table:
134
-
135
-
```sql
136
-
CREATE TABLE [dbo].[AnnualCategorySales]
137
-
( [EnglishProductCategoryName] NVARCHAR(50) NOT NULL
138
-
, [CalendarYear] SMALLINTNOT NULL
139
-
, [TotalSalesAmount] MONEYNOT NULL
140
-
)
141
-
WITH
142
-
(
143
-
DISTRIBUTION = ROUND_ROBIN
144
-
);
145
-
```
146
-
147
-
The original query might have looked something like this example:
148
-
149
-
```sql
150
-
UPDATE acs
151
-
SET [TotalSalesAmount] = [fis].[TotalSalesAmount]
152
-
FROM [dbo].[AnnualCategorySales] AS acs
153
-
JOIN (
154
-
SELECT [EnglishProductCategoryName]
155
-
, [CalendarYear]
156
-
, SUM([SalesAmount]) AS [TotalSalesAmount]
157
-
FROM [dbo].[FactInternetSales] AS s
158
-
JOIN [dbo].[DimDate] AS d ON s.[OrderDateKey] = d.[DateKey]
159
-
JOIN [dbo].[DimProduct] AS p ON s.[ProductKey] = p.[ProductKey]
160
-
JOIN [dbo].[DimProductSubCategory] AS u ON p.[ProductSubcategoryKey] = u.[ProductSubcategoryKey]
161
-
JOIN [dbo].[DimProductCategory] AS c ON u.[ProductCategoryKey] = c.[ProductCategoryKey]
162
-
WHERE [CalendarYear] =2004
163
-
GROUP BY
164
-
[EnglishProductCategoryName]
165
-
, [CalendarYear]
166
-
) AS fis
167
-
ON [acs].[EnglishProductCategoryName] = [fis].[EnglishProductCategoryName]
168
-
AND [acs].[CalendarYear] = [fis].[CalendarYear];
169
-
```
170
-
171
-
Synapse SQL doesn't support ANSI joins in the `FROM` clause of an `UPDATE` statement, so you can't use the previous example without modifying it.
172
-
173
-
You can use a combination of a CTAS and an implicit join to replace the previous example:
174
-
175
-
```sql
176
-
-- Create an interim table
177
-
CREATETABLECTAS_acs
178
-
WITH (DISTRIBUTION = ROUND_ROBIN)
179
-
AS
180
-
SELECT ISNULL(CAST([EnglishProductCategoryName] AS NVARCHAR(50)),0) AS [EnglishProductCategoryName]
181
-
, ISNULL(CAST([CalendarYear] ASSMALLINT),0) AS [CalendarYear]
182
-
, ISNULL(CAST(SUM([SalesAmount]) ASMONEY),0) AS [TotalSalesAmount]
183
-
FROM [dbo].[FactInternetSales] AS s
184
-
JOIN [dbo].[DimDate] AS d ON s.[OrderDateKey] = d.[DateKey]
185
-
JOIN [dbo].[DimProduct] AS p ON s.[ProductKey] = p.[ProductKey]
186
-
JOIN [dbo].[DimProductSubCategory] AS u ON p.[ProductSubcategoryKey] = u.[ProductSubcategoryKey]
187
-
JOIN [dbo].[DimProductCategory] AS c ON u.[ProductCategoryKey] = c.[ProductCategoryKey]
WHERE CTAS_acs.[EnglishProductCategoryName] = AnnualCategorySales.[EnglishProductCategoryName]
197
-
AND CTAS_acs.[CalendarYear] = AnnualCategorySales.[CalendarYear] ;
198
-
199
-
--Drop the interim table
200
-
DROPTABLE CTAS_acs;
201
-
```
202
-
203
-
## ANSI join replacement for MERGE
204
-
205
-
In Azure Synapse Analytics, [MERGE](/sql/t-sql/statements/merge-transact-sql?view=azure-sqldw-latest&preserve-view=true) (preview) with NOT MATCHED BY TARGET requires the target to be a HASH distributed table. Users can use the ANSI JOIN with [UPDATE](/sql/t-sql/queries/update-transact-sql?view=azure-sqldw-latest&preserve-view=true) or [DELETE](/sql/t-sql/statements/delete-transact-sql?view=azure-sqldw-latest&preserve-view=true) as a workaround to modify target table data based on the result from joining with another table. Here is an example.
0 commit comments