共用方式為


教學課程:在倉儲中使用 T-SQL 建立數據表

Applies to:✅ Warehouse in Microsoft Fabric

在本教學課程中,瞭解如何使用 T-SQL 在倉儲中建立數據表。

Note

This tutorial forms part of an end-to-end scenario. 若要完成本教學課程,您必須先完成下列教學課程:

  1. 建立工作區
  2. 建立倉儲
  3. Ingest data into a warehouse

建立數據表

在這項工作中,瞭解如何使用 T-SQL 在倉儲中建立數據表。

  1. Ensure that the workspace you created in the first tutorial is open.

  2. Select the Wide World Importers warehouse (from the items listed on the workspace landing page).

  3. On the Home ribbon, select New SQL query.

    [首頁] 功能區的螢幕快照,強調顯示 [新增 SQL 查詢] 選項。

  4. In the query editor, paste the following code. 程序代碼會卸除 dimension_city 數據表(如果存在),然後建立維度數據表。 它也會刪除 fact_sale 表格(如果存在的話),並且建立事實數據表。

     --Drop the dimension_city table if it already exists.
     DROP TABLE IF EXISTS [dbo].[dimension_city];
    
     --Create the dimension_city table.
     CREATE TABLE [dbo].[dimension_city]
     (
        [CityKey] [int] NULL,
        [WWICityID] [int] NULL,
        [City] [varchar](8000) NULL,
        [StateProvince] [varchar](8000) NULL,
        [Country] [varchar](8000) NULL,
        [Continent] [varchar](8000) NULL,
        [SalesTerritory] [varchar](8000) NULL,
        [Region] [varchar](8000) NULL,
        [Subregion] [varchar](8000) NULL,
        [Location] [varchar](8000) NULL,
        [LatestRecordedPopulation] [bigint] NULL,
        [ValidFrom] [datetime2](6) NULL,
        [ValidTo] [datetime2](6) NULL,
        [LineageKey] [int] NULL
     );
    
     --Drop the fact_sale table if it already exists.
     DROP TABLE IF EXISTS [dbo].[fact_sale];
    
     --Create the fact_sale table.
    CREATE TABLE [dbo].[fact_sale]
    (
       [SaleKey] [bigint] NULL,
       [CityKey] [int] NULL,
       [CustomerKey] [int] NULL,
       [BillToCustomerKey] [int] NULL,
       [StockItemKey] [int] NULL,
       [InvoiceDateKey] [datetime2](6) NULL,
       [DeliveryDateKey] [datetime2](6) NULL,
       [SalespersonKey] [int] NULL,
       [WWIInvoiceID] [int] NULL,
       [Description] [varchar](8000) NULL,
       [Package] [varchar](8000) NULL,
       [Quantity] [int] NULL,
       [UnitPrice] [decimal](18, 2) NULL,
       [TaxRate] [decimal](18, 3) NULL,
       [TotalExcludingTax] [decimal](29, 2) NULL,
       [TaxAmount] [decimal](38, 6) NULL,
       [Profit] [decimal](18, 2) NULL,
       [TotalIncludingTax] [decimal](38, 6) NULL,
       [TotalDryItems] [int] NULL,
       [TotalChillerItems] [int] NULL,
       [LineageKey] [int] NULL,
       [Month] [int] NULL,
       [Year] [int] NULL,
       [Quarter] [int] NULL
    );
    
  5. To execute the query, on the query designer ribbon, select Run.

    查詢編輯器功能區上 [執行] 選項的螢幕快照。

  6. 當文稿執行完成時,若要重新命名查詢,請在 [查詢] 索引標籤上按下滑鼠右鍵,然後選取 [重新命名

    以滑鼠右鍵按兩下 [查詢] 索引標籤時可用的 [重新命名] 選項螢幕快照。

  7. 在 [重新命名] 視窗中,於 [名稱] 方塊中,將預設名稱取代為 Create Tables

    [重新命名] 視窗的螢幕快照,其中顯示輸入的腳本名稱。

  8. 選擇 重新命名

  9. 如有必要,請在 [總管] 窗格中,展開 [架構] 資料夾、[dbo 架構] 和 [數據表] 資料夾。

  10. 確認已列出兩個新的數據表。 dimension_customer 數據表是在 上一個教學課程中建立的。

    [檢視器] 窗格的螢幕截圖,顯示尋找您的表格和新建立查詢的位置。

Next step