Dela via


Självstudie: Klona en tabell med T-SQL i ett lager

Applies to:✅ Warehouse in Microsoft Fabric

I den här självstudien lär du dig hur du klonar en tabell med T-SQL. Mer specifikt lär du dig hur du skapar en tabellkloning med CREATE TABLE AS CLONE OF T-SQL-instruktion.

Note

This tutorial forms part of an end-to-end scenario. För att kunna slutföra den här självstudien måste du först slutföra de här självstudierna:

  1. Skapa en arbetsyta
  2. Skapa ett lager
  3. Ingest data into a Warehouse
  4. Skapa tabeller med T-SQL i ett lager

En klonad tabell ger flera fördelar:

Klona en tabell inom samma schema

I den här uppgiften får du lära dig hur du klonar en tabell i samma schema i lagret.

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

  2. In the Wide World Importers warehouse, on the Home ribbon, select New SQL query.

    Skärmbild av alternativet Ny SQL-fråga i menyfliksområdet.

  3. Klistra in följande kod i frågeredigeraren. Koden skapar en klon av tabellen dimension_city och tabellen fact_sale.

     --Create a clone of the dbo.dimension_city table.
     CREATE TABLE [dbo].[dimension_city1] AS CLONE OF [dbo].[dimension_city];
    
     --Create a clone of the dbo.fact_sale table.
     CREATE TABLE [dbo].[fact_sale1] AS CLONE OF [dbo].[fact_sale];
    
  4. To execute the query, on the query designer ribbon, select Run.

    Skärmbild av alternativet Kör i menyfliksområdet för frågeredigeraren.

  5. När exekveringen är klar, välj i fönstret dimension_city1 för att se den inlästa datan i förhandsvisning.

    Skärmbild av Utforskaren-fönstret som visar tabellen för dimension staden 1.

  6. To create a table clone as of a past point in time, in the query editor, paste the following code to replace the existing statements. Koden skapar en klon av tabellen dimension_city och tabellen fact_sale vid en viss tidpunkt.

     --Create a clone of the dbo.dimension_city table at a specific point in time.   
    CREATE TABLE [dbo].[dimension_city2] AS CLONE OF [dbo].[dimension_city] AT '2025-01-01T10:00:00.000';
    
     --Create a clone of the dbo.fact_sale table at a specific point in time.
    CREATE TABLE [dbo].[fact_sale2] AS CLONE OF [dbo].[fact_sale] AT '2025-01-01T10:00:00.000';
    

    Viktig

    You should replace the timestamp with a past date that is within 30 days of today, but after the date and time (in Coordinated Universal Time—UTC) that you completed the Ingest data into a Warehouse tutorial.

  7. Kör sökfrågan.

  8. When execution completes, preview the data loaded into the fact_sale2 table.

  9. Byt namn på frågan till Clone Tables.

Klona en tabell mellan scheman i samma lager

I den här uppgiften får du lära dig hur du klonar en tabell mellan scheman i samma lager.

  1. Om du vill skapa en ny fråga i menyfliksområdet Start väljer du Ny SQL-fråga.

  2. Klistra in följande kod i frågeredigeraren. Koden skapar ett schema och skapar sedan en klon av tabellen fact_sale och tabellen dimension_city i det nya schemat.

     --Create a new schema within the warehouse named dbo1.
     CREATE SCHEMA dbo1;
     GO
    
     --Create a clone of dbo.fact_sale table in the dbo1 schema.
     CREATE TABLE [dbo1].[fact_sale1] AS CLONE OF [dbo].[fact_sale];
    
     --Create a clone of dbo.dimension_city table in the dbo1 schema.
     CREATE TABLE [dbo1].[dimension_city1] AS CLONE OF [dbo].[dimension_city];
    
  3. Kör sökfrågan.

  4. When execution completes, preview the data loaded into the dimension_city1 table in the dbo1 schema.

  5. Om du vill skapa tabellkloner från och med en tidigare tidpunkti frågeredigeraren klistrar du in följande kod för att ersätta befintliga uttalanden. Koden skapar en klon av tabellen dimension_city och tabellen fact_sale vid vissa tidpunkter i det nya schemat.

    --Create a clone of the dbo.dimension_city table in the dbo1 schema.
    CREATE TABLE [dbo1].[dimension_city2] AS CLONE OF [dbo].[dimension_city] AT '2025-01-01T10:00:00.000';
    
    --Create a clone of the dbo.fact_sale table in the dbo1 schema.
    CREATE TABLE [dbo1].[fact_sale2] AS CLONE OF [dbo].[fact_sale] AT '2025-01-01T10:00:00.000';
    

    Viktig

    You should replace the timestamp with a past date that is within 30 days of today, but after the date and time (in UTC) that you completed the Ingest data into a Warehouse tutorial.

  6. Kör sökfrågan.

  7. When execution completes, preview the data loaded into the fact_sale2 table in the dbo1 schema.

  8. Byt namn på frågan till Clone Tables Across Schemas.

Next step