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
CREATE TABLE AS SELECT (CTAS) is one of the most important T-SQL features available. It is a fully parallelized operation that creates a new table based on the output of a SELECT statement. CTAS is the simplest and fastest way to create a copy of a table.
@@ -26,7 +26,7 @@ CREATE TABLE AS SELECT (CTAS) is one of the most important T-SQL features availa
26
26
- Query or import external data.
27
27
28
28
> [!NOTE]
29
-
> Since CTAS adds to the capabilities of creating a table, this topic tries not to repeat the CREATE TABLE topic. Instead, it describes the differences between the CTAS and CREATE TABLE statements. For the CREATE TABLE details, see [CREATE TABLE (Azure SQL Data Warehouse)](https://msdn.microsoft.com/library/mt203953/) statement.
29
+
> Since CTAS adds to the capabilities of creating a table, this topic tries not to repeat the CREATE TABLE topic. Instead, it describes the differences between the CTAS and CREATE TABLE statements. For the CREATE TABLE details, see [CREATE TABLE (Azure Synapse Analytics)](https://msdn.microsoft.com/library/mt203953/) statement.
30
30
31
31
[Transact-SQL Syntax Conventions](../../t-sql/language-elements/transact-sql-syntax-conventions-transact-sql.md)
CLUSTERED COLUMNSTORE INDEX --default for SQL Data Warehouse
57
+
CLUSTERED COLUMNSTORE INDEX --default for Synapse Analytics
58
58
| CLUSTERED COLUMNSTORE INDEX ORDER (column[,...n])
59
59
| HEAP --default for Parallel Data Warehouse
60
60
| CLUSTERED INDEX ( { index_column_name [ ASC | DESC ] } [ ,...n ] ) --default is ASC
@@ -132,7 +132,7 @@ For details, see [General Remarks](https://msdn.microsoft.com/library/mt203953/#
132
132
133
133
## Limitations and Restrictions
134
134
135
-
An ordered clustered columnstore index can be created on columns of any data types supported in Azure SQL Data Warehouse except for string columns.
135
+
An ordered clustered columnstore index can be created on columns of any data types supported in [!INCLUDE[ssSDW](../../includes/sssdwfull-md.md)] except for string columns.
136
136
137
137
[SET ROWCOUNT (Transact-SQL)](../../t-sql/statements/set-rowcount-transact-sql.md) has no effect on CTAS. To achieve a similar behavior, use [TOP (Transact-SQL)](../../t-sql/queries/top-transact-sql.md).
138
138
@@ -161,7 +161,7 @@ To avoid data movement in subsequent queries, you can specify `REPLICATE` at the
161
161
<aname="ctas-copy-table-bk"></a>
162
162
163
163
### A. Use CTAS to copy a table
164
-
Applies to: Azure SQL Data Warehouse and Parallel Data Warehouse
164
+
Applies to: [!INCLUDE[ssSDW](../../includes/sssdwfull-md.md)]and [!INCLUDE[ssPDW](../../includes/sspdw-md.md)]
165
165
166
166
Perhaps one of the most common uses of `CTAS` is creating a copy of a table so that you can change the DDL. If for example you originally created your table as `ROUND_ROBIN` and now want change it to a table distributed on a column, `CTAS` is how you would change the distribution column. `CTAS` can also be used to change partitioning, indexing, or column types.
167
167
@@ -233,7 +233,7 @@ DROP TABLE FactInternetSales_old;
233
233
<aname="ctas-change-column-attributes-bk"></a>
234
234
235
235
### B. Use CTAS to change column attributes
236
-
Applies to: Azure SQL Data Warehouse and Parallel Data Warehouse
236
+
Applies to: [!INCLUDE[ssSDW](../../includes/sssdwfull-md.md)]and [!INCLUDE[ssPDW](../../includes/sspdw-md.md)]
237
237
238
238
This example uses CTAS to change data types, nullability, and collation for several columns in the DimCustomer2 table.
239
239
@@ -294,13 +294,13 @@ DROP TABLE DimCustomer2_old;
294
294
<aname="ctas-change-distribution-method-bk"></a>
295
295
296
296
### C. Use CTAS to change the distribution method for a table
297
-
Applies to: Azure SQL Data Warehouse and Parallel Data Warehouse
297
+
Applies to: [!INCLUDE[ssSDW](../../includes/sssdwfull-md.md)]and [!INCLUDE[ssPDW](../../includes/sspdw-md.md)]
298
298
299
299
This simple example shows how to change the distribution method for a table. To show the mechanics of how to do this, it changes a hash-distributed table to round-robin and then changes the round-robin table back to hash distributed. The final table matches the original table.
300
300
301
301
In most cases you won't need to change a hash-distributed table to a round-robin table. More often, you might need to change a round-robin table to a hash distributed table. For example, you might initially load a new table as round-robin and then later move it to a hash-distributed table to get better join performance.
302
302
303
-
This example uses the AdventureWorksDW sample database. To load the SQL Data Warehouse version, see [Load sample data into SQL Data Warehouse](https://azure.microsoft.com/documentation/articles/sql-data-warehouse-load-sample-databases/)
303
+
This example uses the AdventureWorksDW sample database. To load the [!INCLUDE[ssSDW](../../includes/sssdwfull-md.md)]version, see [Load sample data into [!INCLUDE[ssSDW](../../includes/sssdwfull-md.md)]](https://azure.microsoft.com/documentation/articles/sql-data-warehouse-load-sample-databases/)
304
304
305
305
```sql
306
306
-- DimSalesTerritory is hash-distributed.
@@ -344,7 +344,7 @@ DROP TABLE [dbo].[DimSalesTerritory_old];
344
344
<aname="ctas-change-to-replicated-bk"></a>
345
345
346
346
### D. Use CTAS to convert a table to a replicated table
347
-
Applies to: Azure SQL Data Warehouse and Parallel Data Warehouse
347
+
Applies to: [!INCLUDE[ssSDW](../../includes/sssdwfull-md.md)]and [!INCLUDE[ssPDW](../../includes/sspdw-md.md)]
348
348
349
349
This example applies for converting round-robin or hash-distributed tables to a replicated table. This particular example takes the previous method of changing the distribution type one step further. Since DimSalesTerritory is a dimension and likely a smaller table, you can choose to re-create the table as replicated to avoid data movement when joining to other tables.
350
350
@@ -368,7 +368,7 @@ DROP TABLE [dbo].[DimSalesTerritory_old];
368
368
```
369
369
370
370
### E. Use CTAS to create a table with fewer columns
371
-
Applies to: Azure SQL Data Warehouse and Parallel Data Warehouse
371
+
Applies to: [!INCLUDE[ssSDW](../../includes/sssdwfull-md.md)]and [!INCLUDE[ssPDW](../../includes/sspdw-md.md)]
372
372
373
373
The following example creates a round-robin distributed table named `myTable (c, ln)`. The new table only has two columns. It uses the column aliases in the SELECT statement for the names of the columns.
374
374
@@ -390,7 +390,7 @@ AS SELECT CustomerKey AS c, LastName AS ln
390
390
<aname="ctas-query-hint-bk"></a>
391
391
392
392
### F. Use a Query Hint with CREATE TABLE AS SELECT (CTAS)
393
-
Applies to: Azure SQL Data Warehouse and Parallel Data Warehouse
393
+
Applies to: [!INCLUDE[ssSDW](../../includes/sssdwfull-md.md)]and [!INCLUDE[ssPDW](../../includes/sspdw-md.md)]
394
394
395
395
This query shows the basic syntax for using a query join hint with the CTAS statement. After the query is submitted, [!INCLUDE[ssSDW](../../includes/sssdw-md.md)] applies the hash join strategy when it generates the query plan for each individual distribution. For more information on the hash join query hint, see [OPTION Clause (Transact-SQL)](../../t-sql/queries/option-clause-transact-sql.md).
396
396
@@ -413,7 +413,7 @@ OPTION ( HASH JOIN );
413
413
<aname="ctas-azure-blob-storage-bk"></a>
414
414
415
415
### G. Use CTAS to import data from Azure Blob storage
416
-
Applies to: Azure SQL Data Warehouse and Parallel Data Warehouse
416
+
Applies to: [!INCLUDE[ssSDW](../../includes/sssdwfull-md.md)]and [!INCLUDE[ssPDW](../../includes/sspdw-md.md)]
417
417
418
418
To import data from an external table, simply use CREATE TABLE AS SELECT to select from the external table. The syntax to select data from an external table into [!INCLUDE[ssSDW](../../includes/sssdw-md.md)] is the same as the syntax for selecting data from a regular table.
419
419
@@ -434,7 +434,7 @@ WITH (
434
434
;
435
435
436
436
--Use CREATE TABLE AS SELECT to import the Azure blob storage data into a new
437
-
--SQL Data Warehouse table called ClickStreamData
437
+
--Synapse Analytics table called ClickStreamData
438
438
CREATETABLEClickStreamData
439
439
WITH
440
440
(
@@ -495,7 +495,7 @@ Use CTAS to work around some unsupported features. Besides being able to run you
495
495
<aname="ctas-replace-select-into-bk"></a>
496
496
497
497
### I. Use CTAS instead of SELECT..INTO
498
-
Applies to: Azure SQL Data Warehouse and Parallel Data Warehouse
498
+
Applies to: [!INCLUDE[ssSDW](../../includes/sssdwfull-md.md)]and [!INCLUDE[ssPDW](../../includes/sspdw-md.md)]
499
499
500
500
SQL Server code typically uses SELECT..INTO to populate a table with the results of a SELECT statement. This is an example of a SQL Server SELECT..INTO statement.
501
501
@@ -505,7 +505,7 @@ INTO #tmp_fct
505
505
FROM [dbo].[FactInternetSales]
506
506
```
507
507
508
-
This syntax is not supported in SQL Data Warehouse and Parallel Data Warehouse. This example shows how to rewrite the previous SELECT..INTO statement as a CTAS statement. You can choose any of the DISTRIBUTION options described in the CTAS syntax. This example uses the ROUND_ROBIN distribution method.
508
+
This syntax is not supported in [!INCLUDE[ssSDW](../../includes/sssdwfull-md.md)] and Parallel Data Warehouse. This example shows how to rewrite the previous SELECT..INTO statement as a CTAS statement. You can choose any of the DISTRIBUTION options described in the CTAS syntax. This example uses the ROUND_ROBIN distribution method.
509
509
510
510
```sql
511
511
CREATE TABLE #tmp_fct
@@ -522,7 +522,7 @@ FROM [dbo].[FactInternetSales]
522
522
<aname="ctas-replace-implicit-joins-bk"></a>
523
523
524
524
### J. Use CTAS and implicit joins to replace ANSI joins in the `FROM` clause of an `UPDATE` statement
525
-
Applies to: Azure SQL Data Warehouse and Parallel Data Warehouse
525
+
Applies to: [!INCLUDE[ssSDW](../../includes/sssdwfull-md.md)]and [!INCLUDE[ssPDW](../../includes/sspdw-md.md)]
526
526
527
527
You may find you have a complex update that joins more than two tables together using ANSI joining syntax to perform the UPDATE or DELETE.
528
528
@@ -566,7 +566,7 @@ AND [acs].[CalendarYear] = [fis].[CalendarYear]
566
566
;
567
567
```
568
568
569
-
Since SQL Data Warehouse does not support ANSI joins in the `FROM` clause of an `UPDATE` statement, you cannot use this SQL Server code over without changing it slightly.
569
+
Since [!INCLUDE[ssSDW](../../includes/sssdwfull-md.md)] does not support ANSI joins in the `FROM` clause of an `UPDATE` statement, you cannot use this SQL Server code over without changing it slightly.
570
570
571
571
You can use a combination of a `CTAS` and an implicit join to replace this code:
572
572
@@ -605,9 +605,9 @@ DROP TABLE CTAS_acs
605
605
<aname="ctas-replace-ansi-joins-bk"></a>
606
606
607
607
### K. Use CTAS to specify which data to keep instead of using ANSI joins in the FROM clause of a DELETE statement
608
-
Applies to: Azure SQL Data Warehouse and Parallel Data Warehouse
608
+
Applies to: [!INCLUDE[ssSDW](../../includes/sssdwfull-md.md)]and [!INCLUDE[ssPDW](../../includes/sspdw-md.md)]
609
609
610
-
Sometimes the best approach for deleting data is to use `CTAS`. Rather than deleting the data simply select the data you want to keep. This especially true for `DELETE` statements that use ansi joining syntax since SQL Data Warehouse does not support ANSI joins in the `FROM` clause of a `DELETE` statement.
610
+
Sometimes the best approach for deleting data is to use `CTAS`. Rather than deleting the data simply select the data you want to keep. This especially true for `DELETE` statements that use ansi joining syntax since [!INCLUDE[ssSDW](../../includes/sssdwfull-md.md)] does not support ANSI joins in the `FROM` clause of a `DELETE` statement.
611
611
612
612
An example of a converted DELETE statement is available below:
613
613
@@ -633,7 +633,7 @@ RENAME OBJECT dbo.DimProduct_upsert TO DimProduct;
633
633
<aname="ctas-simplify-merge-bk"></a>
634
634
635
635
### L. Use CTAS to simplify merge statements
636
-
Applies to: Azure SQL Data Warehouse and Parallel Data Warehouse
636
+
Applies to: [!INCLUDE[ssSDW](../../includes/sssdwfull-md.md)]and [!INCLUDE[ssPDW](../../includes/sspdw-md.md)]
637
637
638
638
Merge statements can be replaced, at least in part, by using `CTAS`. You can consolidate the `INSERT` and the `UPDATE` into a single statement. Any deleted records would need to be closed off in a second statement.
639
639
@@ -672,9 +672,9 @@ RENAME OBJECT dbo.[DimProduct_upsert] TO [DimProduct];
672
672
<aname="ctas-data-type-and-nullability-bk"></a>
673
673
674
674
### M. Explicitly state data type and nullability of output
675
-
Applies to: Azure SQL Data Warehouse and Parallel Data Warehouse
675
+
Applies to: [!INCLUDE[ssSDW](../../includes/sssdwfull-md.md)]and [!INCLUDE[ssPDW](../../includes/sspdw-md.md)]
676
676
677
-
When migrating SQL Server code to SQL Data Warehouse, you might find you run across this type of coding pattern:
677
+
When migrating SQL Server code to [!INCLUDE[ssSDW](../../includes/sssdwfull-md.md)], you might find you run across this type of coding pattern:
678
678
679
679
```sql
680
680
DECLARE @d DECIMAL(7,2) =85.455
@@ -838,7 +838,7 @@ OPTION (MAXDOP 1);
838
838
[CREATE EXTERNAL FILE FORMAT (Transact-SQL)](../../t-sql/statements/create-external-file-format-transact-sql.md)
0 commit comments