Udostępnij przez


Samouczek: przekształcanie danych za pomocą procedury składowanej w magazynie

Applies to:✅ SQL analytics endpoint and Warehouse in Microsoft Fabric

Z tego samouczka dowiesz się, jak utworzyć procedurę składowaną w magazynie w celu przekształcenia danych w tabeli.

Notatka

This tutorial forms part of an end-to-end scenario. Aby ukończyć ten samouczek, należy najpierw wykonać następujące samouczki:

  1. Tworzenie obszaru roboczego
  2. Tworzenie magazynu
  3. Ingest data into a Warehouse

Create a stored procedure

In this task, learn how to create a stored procedure to transform data in a warehouse table.

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

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

    Zrzut ekranu przedstawiający wstążkę Startową z wyróżnioną opcją Nowe zapytanie SQL.

  3. In the query editor, paste the following code. Kod pominie procedurę składowaną (jeśli istnieje), a następnie tworzy procedurę składowaną o nazwie populate_aggregate_sale_by_city. Logika procedury składowanej tworzy tabelę o nazwie aggregate_sale_by_date_city i wstawia do niej dane za pomocą zapytania grupowania, które łączy tabele fact_sale i dimension_city.

     --Drop the stored procedure if it already exists.
     DROP PROCEDURE IF EXISTS [dbo].[populate_aggregate_sale_by_city];
     GO
    
     --Create the populate_aggregate_sale_by_city stored procedure.
     CREATE PROCEDURE [dbo].[populate_aggregate_sale_by_city]
     AS
     BEGIN
         --Drop the aggregate table if it already exists.
         DROP TABLE IF EXISTS [dbo].[aggregate_sale_by_date_city];
         --Create the aggregate table.
         CREATE TABLE [dbo].[aggregate_sale_by_date_city]
         (
            [Date] [DATETIME2](6),
            [City] [VARCHAR](8000),
            [StateProvince] [VARCHAR](8000),
            [SalesTerritory] [VARCHAR](8000),
            [SumOfTotalExcludingTax] [DECIMAL](38,2),
            [SumOfTaxAmount] [DECIMAL](38,6),
            [SumOfTotalIncludingTax] [DECIMAL](38,6),
            [SumOfProfit] [DECIMAL](38,2)
         );
    
         --Load aggregated data into the table.
         INSERT INTO [dbo].[aggregate_sale_by_date_city]
         SELECT
            FS.[InvoiceDateKey] AS [Date], 
            DC.[City], 
            DC.[StateProvince], 
            DC.[SalesTerritory], 
            SUM(FS.[TotalExcludingTax]) AS [SumOfTotalExcludingTax], 
            SUM(FS.[TaxAmount]) AS [SumOfTaxAmount], 
            SUM(FS.[TotalIncludingTax]) AS [SumOfTotalIncludingTax], 
            SUM(FS.[Profit]) AS [SumOfProfit]
         FROM [dbo].[fact_sale] AS FS
         INNER JOIN [dbo].[dimension_city] AS DC
            ON FS.[CityKey] = DC.[CityKey]
         GROUP BY
            FS.[InvoiceDateKey],
            DC.[City], 
            DC.[StateProvince], 
            DC.[SalesTerritory]
         ORDER BY 
            FS.[InvoiceDateKey], 
            DC.[StateProvince], 
            DC.[City];
     END;
    
  4. Aby wykonać zapytanie, na wstążce projektanta zapytań wybierz pozycję Uruchom.

  5. Po zakończeniu wykonywania zmień nazwę zapytania na Create Aggregate Procedure.

  6. In the Explorer pane, from inside the Stored Procedures folder for the dbo schema, verify that the aggregate_sale_by_date_city stored procedure exists.

    Screenshot of the Explorer pane, highlighting the newly created stored procedure.

Run the stored procedure

In this task, learn how to execute the stored procedure to transform data in a warehouse table.

  1. Utwórz nowe zapytanie.

  2. In the query editor, paste the following code. Kod wykonuje składowaną procedurę o nazwie populate_aggregate_sale_by_city.

     --Execute the stored procedure to create and load aggregated data.
     EXEC [dbo].[populate_aggregate_sale_by_city];
    
  3. Uruchom zapytanie.

  4. Po zakończeniu wykonywania zmień nazwę zapytania na Run Aggregate Procedure.

  5. Aby wyświetlić podgląd zagregowanych danych, w okienku Eksploratora wybierz tabelę .

    Notatka

    If the table doesn't appear, select the ellipsis (…) for the Tables folder, and then select Refresh.

    Zrzut ekranu przedstawiający okienko Eksplorator z wyróżnioną nowo utworzoną tabelą.

Next step