자습서: 저장 프로시저를 사용하여 데이터 변환

적용 대상: Microsoft Fabric의 SQL 분석 엔드포인트 및 웨어하우스

데이터를 변환하는 새 저장 프로시저를 만들고 저장하는 방법을 알아봅니다.

데이터 변환

  1. 리본의 탭에서 새 SQL 쿼리를 선택합니다.

    Screenshot of the ribbon of the Home tab, showing where to select New SQL query.

  2. 쿼리 편집기에서 다음 코드를 붙여넣어 저장 프로시저 dbo.populate_aggregate_sale_by_city를 만듭니다. 이 저장 프로시저는 이후 단계에서 테이블을 만들고 로드 dbo.aggregate_sale_by_date_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
        --If the aggregate table already exists, drop it. Then create the table.
        DROP TABLE IF EXISTS [dbo].[aggregate_sale_by_date_city];
        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)
            );
    
        --Reload the aggregated dataset to 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
    
  3. 나중에 참조를 위해 이 쿼리를 저장하려면 쿼리 탭을 마우스 오른쪽 단추로 클릭하고 이름 바꾸기를 선택합니다.

    Screenshot of the tabs in the editor screen, showing where to right-click on the query and select Rename.

  4. 집계 프로시저 만들기를 입력하여 쿼리의 이름을 변경합니다.

  5. 키보드에서 Enter 키를 누르거나 탭 외부의 아무 곳이나 선택하여 변경 사항을 저장합니다.

  6. 실행을 선택하여 쿼리를 실행합니다.

  7. 리본에서 새로 고침 단추를 선택합니다.

    Screenshot of the Home ribbon, showing where to select the Refresh button.

  8. 개체 탐색기에서 스키마 아래에 dbo 있는 StoredProcedures 노드를 확장하여 새로 만든 저장 프로시저를 볼 수 있는지 확인합니다.

    Screenshot of the Explorer pane, showing where to expand the StoredProcedures node to find your newly created procedure.

  9. 리본의 탭에서 새 SQL 쿼리를 선택합니다.

  10. 쿼리 편집기에서 다음 코드를 붙여넣습니다. 이 T-SQL은 dbo.populate_aggregate_sale_by_city 테이블을 만들기 dbo.aggregate_sale_by_date_city 위해 실행됩니다.

    --Execute the stored procedure to create the aggregate table.
    EXEC [dbo].[populate_aggregate_sale_by_city];
    
  11. 나중에 참조를 위해 이 쿼리를 저장하려면 쿼리 탭을 마우스 오른쪽 단추로 클릭하고 이름 바꾸기를 선택합니다.

  12. 실행 집계 프로시저를 입력하여 쿼리의 이름을 변경합니다.

  13. 키보드에서 Enter 키를 누르거나 탭 외부의 아무 곳이나 선택하여 변경 사항을 저장합니다.

  14. 실행을 선택하여 쿼리를 실행합니다.

  15. 리본에서 새로 고침 단추를 선택합니다. 쿼리를 실행하는 데 2~3분이 걸립니다.

  16. 개체 탐색기에서 데이터 미리 보기를 로드하여 탐색기에서 테이블을 선택하여 로드된 데이터의 유효성을 aggregate_sale_by_city검사합니다.

    Screenshot of the Explorer pane next to a Data preview screen that lists the data loaded into the selected table.

다음 단계