Skip to content

Commit f49c426

Browse files
authored
Merge pull request #20507 from ajagadish-24/patch-1
20211020 Addition of Column Rename Feature to CETAS
2 parents c28c33d + d432d25 commit f49c426

1 file changed

Lines changed: 38 additions & 3 deletions

File tree

docs/t-sql/statements/create-external-table-as-select-transact-sql.md

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
description: "CREATE EXTERNAL TABLE AS SELECT (Transact-SQL)"
33
title: "CREATE EXTERNAL TABLE AS SELECT (Transact-SQL) | Microsoft Docs"
44
ms.custom: ""
5-
ms.date: "08/10/2017"
5+
ms.date: "10/20/2021"
66
ms.prod_service: "synapse-analytics, pdw"
77
ms.reviewer: ""
88
ms.prod: sql
@@ -31,7 +31,8 @@ monikerRange: ">= aps-pdw-2016 || = azure-sqldw-latest"
3131
## Syntax
3232

3333
```syntaxsql
34-
CREATE EXTERNAL TABLE [ [database_name . [ schema_name ] . ] | schema_name . ] table_name
34+
CREATE EXTERNAL TABLE {[ [database_name . [ schema_name ] . ] | schema_name . ] table_name }
35+
[(column_name [,...n ] ) ]
3536
WITH (
3637
LOCATION = 'hdfs_folder',
3738
DATA_SOURCE = external_data_source_name,
@@ -55,7 +56,10 @@ CREATE EXTERNAL TABLE [ [database_name . [ schema_name ] . ] | schema_name . ]
5556

5657
## Arguments
5758
**[ [ *database_name* . [ *schema_name* ] . ] | *schema_name* . ] *table_name***
58-
is the one- to three-part name of the table to create in the database. For an external table, only the table metadata is stored in the relational database.
59+
is the one- to three-part name of the table to create in the database. For an external table, only the table metadata is stored in the relational database.
60+
61+
**[ ( column_name [ ,...n ] ) ]**
62+
is the name of a table column.
5963

6064
**LOCATION = '*hdfs_folder*'**
6165
specifies where to write the results of the SELECT statement on the external data source. The location is a folder name and can optionally include a path that's relative to the root folder of the Hadoop cluster or Blob storage. PolyBase will create the path and folder if it doesn't already exist.
@@ -108,6 +112,13 @@ The external files are written to *hdfs_folder* and named *QueryID_date_time_ID.
108112
**SELECT \<select_criteria>**
109113
populates the new table with the results from a SELECT statement. *select_criteria* is the body of the SELECT statement that determines which data to copy to the new table. For information about SELECT statements, see [SELECT &#40;Transact-SQL&#41;](../../t-sql/queries/select-transact-sql.md).
110114

115+
**Column options**
116+
117+
column_name [ ,...n ]
118+
Column names do not allow the column options mentioned in CREATE TABLE. Instead, you can provide an optional list of one or more column names for the new table. The columns in the new table will use the names you specify. When you specify column names, the number of columns in the column list must match the number of columns in the select results. If you don't specify any column names, the new target table will use the column names in the select statement results.
119+
120+
You cannot specify any other column options such as data types, collation, or nullability. Each of these attributes is derived from the results of the SELECT statement. However, you can use the SELECT statement to change the attributes. For an example, see [Use CETAS to change column attributes](#c-use-cetas-to-change-column-attributes).
121+
111122
## Permissions
112123

113124
To run this command, the *database user* needs all of these permissions or memberships:
@@ -234,6 +245,30 @@ ON ( T1.CustomerKey = T2.CustomerKey )
234245
OPTION ( HASH JOIN );
235246
```
236247

248+
### C. Use CETAS to change column attributes
249+
This example uses CETAS to change data types, nullability, and collation for several columns in the `FactInternetSales` table.
250+
251+
```sql
252+
-- Example is based on AdventureWorks
253+
CREATE EXTERNAL TABLE dbo.FactInternetSalesNew
254+
WITH
255+
(
256+
LOCATION = '/files/Customer',
257+
DATA_SOURCE = customer_ds,
258+
FILE_FORMAT = customer_ff
259+
)
260+
AS SELECT T1.ProductKey AS ProductKeyNoChange,
261+
T1.OrderDateKey AS OrderDate,
262+
T1.ShipDateKey AS ShipDate,
263+
T1.CustomerKey AS CustomerKeyNoChange,
264+
T1.OrderQuantity AS Quantity,
265+
T1.SalesAmount AS Money
266+
FROM dbo.FactInternetSales T1 JOIN dbo.DimCustomer T2
267+
ON ( T1.CustomerKey = T2.CustomerKey )
268+
OPTION ( HASH JOIN );
269+
```
270+
271+
237272
## See also
238273
- [CREATE EXTERNAL DATA SOURCE &#40;Transact-SQL&#41;](../../t-sql/statements/create-external-data-source-transact-sql.md)
239274
- [CREATE EXTERNAL FILE FORMAT &#40;Transact-SQL&#41;](../../t-sql/statements/create-external-file-format-transact-sql.md)

0 commit comments

Comments
 (0)