Snowflake SQL Tutorial 134 - I Cloned A Schema And One View Kept Pointing At The Original

Snowflake SQL · Episode 134

In this post we are going to cover zero-copy cloning in Snowflake.

A clone is a second name over data that is already stored. It is not a copy, because nothing is copied. It is not a pointer either, because the two names go their own way the moment either side is written to.

We will start by cloning one small table and build it up step by step, through a clone of the past, a whole schema, and a whole database. Every query below was run on a real Snowflake account, and every result you see is the output it gave back.

Episode 134 of the Snowflake SQL: Zero to Hero series on TechBrothersIT.

Let's create some sample data

We need a table with something to carry, so that we can see what travels to a clone and what does not. This one has a NOT NULL, a DEFAULT, a clustering key and a comment.

Sample data
-- Sample data for this post.
-- A table with a few things on it worth watching: a NOT NULL, a DEFAULT,
-- a clustering key and a comment.
USE DATABASE DEMO_DB;
USE SCHEMA PUBLIC;

CREATE OR REPLACE TABLE SALES_T134 (
    SALE_ID INT NOT NULL,
    REGION  VARCHAR(10) DEFAULT 'unknown',
    AMOUNT  NUMBER(10,2)
)
CLUSTER BY (REGION)
COMMENT = 'the source table for episode 134';

INSERT INTO SALES_T134 (SALE_ID, REGION, AMOUNT)
VALUES (1, 'north', 100.00), (2, 'south', 250.50),
       (3, 'east',   75.25), (4, 'west',  310.00),
       (5, 'north',  45.00), (6, 'south',  88.25);

Let's see what is in it.

Query
SELECT COUNT(*)    AS ROWS_IN_THE_TABLE,
       SUM(AMOUNT) AS TOTAL_AMOUNT
FROM SALES_T134;
Result1 row · 2 columns
ROWS_IN_THE_TABLETOTAL_AMOUNT
6869.00

Six rows and 869.00. Those are the two numbers to watch for the rest of this post.

Let's clone it

One statement, and no options to get wrong.

Query
CREATE OR REPLACE TABLE SALES_CLONE_T134 CLONE SALES_T134;
Result1 row · 1 column
status
Table SALES_CLONE_T134 successfully created.

Now let's ask the clone what it holds.

Query
SELECT COUNT(*)    AS ROWS_IN_THE_CLONE,
       SUM(AMOUNT) AS TOTAL_AMOUNT
FROM SALES_CLONE_T134;
Result1 row · 2 columns
ROWS_IN_THE_CLONETOTAL_AMOUNT
6869.00

Six rows and 869.00. The same two numbers.

And the rows themselves, so there is no doubt about it.

Query
SELECT SALE_ID, REGION, AMOUNT
FROM SALES_CLONE_T134
ORDER BY SALE_ID;
Result6 rows · 3 columns
SALE_IDREGIONAMOUNT
1north100.00
2south250.50
3east75.25
4west310.00
5north45.00
6south88.25

That part is not a surprise. The interesting question is what the catalog now thinks it is looking after.

Now let's ask the catalog what the two of them weigh

SHOW TABLES gives us both of them side by side.

Query
SHOW TABLES LIKE '%\\_T134' IN SCHEMA PUBLIC;
Result2 rows · 27 columns · scroll →
created_onnamedatabase_nameschema_namekindcommentcluster_byrowsbytesownerretention_timeautomatic_clusteringchange_trackingsearch_optimizationsearch_optimization_progresssearch_optimization_bytesis_externalenable_schema_evolutionowner_role_typeis_eventis_hybridis_icebergis_dynamicis_immutableis_interactiverow_timestamperror_logging
2026-10-02 08:45:22.951000-07:00SALES_CLONE_T134DEMO_DBPUBLICTABLEthe source table for episode 134LINEAR(REGION)61024ACCOUNTADMIN1OFFOFFOFFNULLNULLNNROLENNNNNNOFFOFF
2026-10-02 08:45:21.238000-07:00SALES_T134DEMO_DBPUBLICTABLEthe source table for episode 134LINEAR(REGION)61024ACCOUNTADMIN1ONOFFOFFNULLNULLNNROLENNNNNNOFFOFF

Twenty seven columns, which is a lot to read at once. Start with rows and bytes. Six rows and 1024 bytes, for each of them.

The same 1024 bytes, twice, for data that is stored once. Zero-copy is real, and the catalog bills it to both names.

Now read down the column headings. There is no column that says this one is a clone. A clone is indistinguishable here from a table somebody built the long way.

There is one honest difference and it is easy to miss. automatic_clustering is ON for the source and OFF for the clone. The clustering key travelled. The service that acts on it did not.

Empty is not NULL

While that grid is in front of you, have a look at its blank-looking cells. search_optimization_progress and search_optimization_bytes are real NULLs, and the grid prints them as NULL. SHOW output also uses empty strings for missing values, and the grid marks those as empty instead. Two different things that both look like nothing.

The catalog view answers the same question in fewer columns.

Query
SELECT TABLE_NAME,
       TABLE_TYPE,
       IS_TRANSIENT,
       ROW_COUNT,
       BYTES,
       RETENTION_TIME,
       CLUSTERING_KEY,
       COMMENT
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'PUBLIC'
  AND ENDSWITH(TABLE_NAME, '_T134')
ORDER BY TABLE_NAME;
Result2 rows · 8 columns · scroll →
TABLE_NAMETABLE_TYPEIS_TRANSIENTROW_COUNTBYTESRETENTION_TIMECLUSTERING_KEYCOMMENT
SALES_CLONE_T134BASE TABLENO610241LINEAR(REGION)the source table for episode 134
SALES_T134BASE TABLENO610241LINEAR(REGION)the source table for episode 134

Same row count, same byte count, same clustering key, same comment, and neither of them is transient. The comment came across with the clone, which is worth noticing: the comment says the source table for episode 134 on a table that is not the source.

And if you want to see exactly what came with it, ask for the DDL of each one.

The source
SELECT GET_DDL('TABLE', 'SALES_T134') AS THE_SOURCE_DDL;
Result1 row · 1 column
THE_SOURCE_DDL
create or replace TABLE SALES_T134 cluster by (REGION)( SALE_ID NUMBER(38,0) NOT NULL, REGION VARCHAR(10) DEFAULT 'unknown', AMOUNT NUMBER(10,2) )COMMENT='the source table for episode 134' ;
The clone
SELECT GET_DDL('TABLE', 'SALES_CLONE_T134') AS THE_CLONE_DDL;
Result1 row · 1 column
THE_CLONE_DDL
create or replace TABLE SALES_CLONE_T134 cluster by (REGION)( SALE_ID NUMBER(38,0) NOT NULL, REGION VARCHAR(10) DEFAULT 'unknown', AMOUNT NUMBER(10,2) )COMMENT='the source table for episode 134' ;

Two identical definitions apart from the name. The NOT NULL, the DEFAULT, the cluster by and the comment are all in the clone's own DDL.

Now let's write to one side only

Before we change anything, let's keep hold of this moment. We are going to want to come back to it later.

Query
SET QID_SIX = LAST_QUERY_ID();
Result1 row · 1 column
status
Statement executed successfully.

That variable now holds the id of the statement that ran just before it. The grid that prints the id itself is left off this page, because a query id belongs to one account's own history and there is nothing to learn from reading it.

Now a row into the clone, and nothing into the source.

Query
INSERT INTO SALES_CLONE_T134 (SALE_ID, REGION, AMOUNT)
VALUES (7, 'east', 500.00);
Result1 row · 1 column
number of rows inserted
1
The clone
SELECT COUNT(*)    AS ROWS_IN_THE_CLONE,
       SUM(AMOUNT) AS TOTAL_AMOUNT
FROM SALES_CLONE_T134;
Result1 row · 2 columns
ROWS_IN_THE_CLONETOTAL_AMOUNT
71369.00
The source
SELECT COUNT(*)    AS ROWS_IN_THE_SOURCE,
       SUM(AMOUNT) AS TOTAL_AMOUNT
FROM SALES_T134;
Result1 row · 2 columns
ROWS_IN_THE_SOURCETOTAL_AMOUNT
6869.00

The clone is up to seven rows and 1369.00. The source is still six rows and 869.00. It did not notice.

Now the other direction. Let's change the source instead.

Query
UPDATE SALES_T134 SET AMOUNT = 0 WHERE REGION = 'south';
Result1 row · 2 columns
number of rows updatednumber of multi-joined rows updated
20
The source
SELECT COUNT(*)    AS ROWS_IN_THE_SOURCE,
       SUM(AMOUNT) AS TOTAL_AMOUNT
FROM SALES_T134;
Result1 row · 2 columns
ROWS_IN_THE_SOURCETOTAL_AMOUNT
6530.25
The clone
SELECT COUNT(*)    AS ROWS_IN_THE_CLONE,
       SUM(AMOUNT) AS TOTAL_AMOUNT
FROM SALES_CLONE_T134;
Result1 row · 2 columns
ROWS_IN_THE_CLONETOTAL_AMOUNT
71369.00

Two rows updated, so the source is six rows and 530.25 now. The clone is still seven rows and 1369.00.

Let's put them next to each other and label every row, because that is the grid you actually want to see.

Query
SELECT COALESCE(s.SALE_ID, c.SALE_ID) AS SALE_ID,
       s.AMOUNT                       AS IN_THE_SOURCE,
       c.AMOUNT                       AS IN_THE_CLONE,
       CASE WHEN s.SALE_ID IS NULL                 THEN 'CLONE ONLY'
            WHEN c.SALE_ID IS NULL                 THEN 'SOURCE ONLY'
            WHEN s.AMOUNT <> c.AMOUNT              THEN 'DIFFERENT'
            ELSE                                        'same'
       END                            AS VERDICT
FROM       SALES_T134       s
FULL OUTER JOIN SALES_CLONE_T134 c ON s.SALE_ID = c.SALE_ID
ORDER BY SALE_ID;
Result7 rows · 4 columns
SALE_IDIN_THE_SOURCEIN_THE_CLONEVERDICT
1100.00100.00same
20.00250.50DIFFERENT
375.2575.25same
4310.00310.00same
545.0045.00same
60.0088.25DIFFERENT
7NULL500.00CLONE ONLY

Sale 2 and sale 6 are different, because those are the two south rows we zeroed in the source. Sale 7 is in the clone only. Everything else is the same on both sides.

What happens when we drop the source?

This is the part a copy and a pointer would answer differently, so it is the part worth doing carefully.

First let's make the two sides easy to tell apart. Two rows out of the source.

Query
DELETE FROM SALES_T134 WHERE SALE_ID IN (5, 6);
Result1 row · 1 column
number of rows deleted
2
The source
SELECT COUNT(*) AS ROWS_IN_THE_SOURCE FROM SALES_T134;
Result1 row · 1 column
ROWS_IN_THE_SOURCE
4
The clone
SELECT COUNT(*) AS ROWS_IN_THE_CLONE FROM SALES_CLONE_T134;
Result1 row · 1 column
ROWS_IN_THE_CLONE
7

Four rows on one side and seven on the other. Now let's drop the source entirely.

Query
DROP TABLE SALES_T134;
Result1 row · 1 column
status
SALES_T134 successfully dropped.

And select from it, to be sure it really is gone.

Query
SELECT COUNT(*) AS ROWS_IN_THE_SOURCE FROM SALES_T134;
Query failed — Snowflake returned an error
002003 (42S02): SQL compilation error:
Object 'SALES_T134' does not exist or not authorized. Your primary role ACCOUNTADMIN or one of your secondary roles must have at least one privilege granted on TABLE SALES_T134.

002003, does not exist or not authorized. The source is gone. Now let's read the clone.

Query
SELECT COUNT(*)    AS ROWS_IN_THE_CLONE,
       SUM(AMOUNT) AS TOTAL_AMOUNT
FROM SALES_CLONE_T134;
Result1 row · 2 columns
ROWS_IN_THE_CLONETOTAL_AMOUNT
71369.00

Seven rows and 1369.00. It is still there.

The clone outlives the table it was made from, because the storage underneath was never the source's to own.

And the source is only dropped, not destroyed, so we can have it back.

Query
UNDROP TABLE SALES_T134;
Result1 row · 1 column
status
Table SALES_T134 successfully restored.
Query
SELECT COUNT(*)    AS ROWS_IN_THE_SOURCE,
       SUM(AMOUNT) AS TOTAL_AMOUNT
FROM SALES_T134;
Result1 row · 2 columns
ROWS_IN_THE_SOURCETOTAL_AMOUNT
4485.25

Four rows and 485.25, which is where we left it. Both of them exist again, side by side.

So what else comes across, and when is that decided?

Retention is a good one to ask about, because it is a setting rather than data. Let's raise it on the source and then clone the source a second time.

Query
ALTER TABLE SALES_T134 SET DATA_RETENTION_TIME_IN_DAYS = 3;
Result1 row · 1 column
status
Statement executed successfully.
Query
CREATE OR REPLACE TABLE SALES_CLONE2_T134 CLONE SALES_T134;
Result1 row · 1 column
status
Table SALES_CLONE2_T134 successfully created.

So now there are three tables. The source, a clone made before the change, and a clone made after it.

Query
SELECT TABLE_NAME, RETENTION_TIME, CLUSTERING_KEY, COMMENT
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'PUBLIC'
  AND TABLE_NAME IN ('SALES_T134', 'SALES_CLONE_T134', 'SALES_CLONE2_T134')
ORDER BY TABLE_NAME;
Result3 rows · 4 columns
TABLE_NAMERETENTION_TIMECLUSTERING_KEYCOMMENT
SALES_CLONE2_T1343LINEAR(REGION)the source table for episode 134
SALES_CLONE_T1341LINEAR(REGION)the source table for episode 134
SALES_T1343LINEAR(REGION)the source table for episode 134

Three answers. The source says 3. The clone made after the change says 3. The first clone, made before it, still says 1.

So retention is copied at the moment you clone, and then the two settings go their own way. Changing the source later does not reach a clone that already exists.

Can a transient table be cloned into a permanent one?

Let's make a transient table and clone it without saying the word transient.

A transient table
CREATE OR REPLACE TRANSIENT TABLE TRANS_T134 (
    SALE_ID INT,
    AMOUNT  NUMBER(10,2)
);
Result1 row · 1 column
status
Table TRANS_T134 successfully created.
Two rows in it
INSERT INTO TRANS_T134 (SALE_ID, AMOUNT) VALUES (1, 10.00), (2, 20.00);
Result1 row · 1 column
number of rows inserted
2
Query
CREATE OR REPLACE TABLE TRANSCLONE_T134 CLONE TRANS_T134;
Query failed — Snowflake returned an error
002120 (0A000): SQL compilation error: Transient object cannot be cloned to a permanent object.

002120, Transient object cannot be cloned to a permanent object. Refused.

Let's ask the catalog which of those two tables exists.

Query
SELECT TABLE_NAME, TABLE_TYPE, IS_TRANSIENT, RETENTION_TIME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'PUBLIC'
  AND TABLE_NAME IN ('TRANS_T134', 'TRANSCLONE_T134')
ORDER BY TABLE_NAME;
Result1 row · 4 columns
TABLE_NAMETABLE_TYPEIS_TRANSIENTRETENTION_TIME
TRANS_T134BASE TABLEYES1

One row. The transient table is there and the clone was never created, which is what a compilation error means.

Now the other direction. A transient clone of a permanent table.

Query
CREATE OR REPLACE TRANSIENT TABLE PERMCLONE_T134 CLONE SALES_T134;
Result1 row · 1 column
status
Table PERMCLONE_T134 successfully created.
Query
SELECT TABLE_NAME, TABLE_TYPE, IS_TRANSIENT, RETENTION_TIME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'PUBLIC'
  AND TABLE_NAME = 'PERMCLONE_T134';
Result1 row · 4 columns
TABLE_NAMETABLE_TYPEIS_TRANSIENTRETENTION_TIME
PERMCLONE_T134BASE TABLEYES1

Accepted, and IS_TRANSIENT says YES.

So it is a one-way door. Permanent to transient is fine, and transient to permanent is refused. Which makes sense once you say it out loud: you cannot promise more durability than the data ever had.

Can we clone the table as it used to be?

Yes, and this is the one worth knowing. Remember the statement id we kept earlier, from before the update. Let's clone the source as it was before that statement ran.

Query
CREATE OR REPLACE TABLE PAST_T134 CLONE SALES_T134
BEFORE (STATEMENT => $QID_SIX);
Result1 row · 1 column
status
Table PAST_T134 successfully created.
Query
SELECT COUNT(*)    AS ROWS_IN_IT,
       SUM(AMOUNT) AS TOTAL_AMOUNT
FROM PAST_T134;
Result1 row · 2 columns
ROWS_IN_ITTOTAL_AMOUNT
6869.00

Six rows and 869.00. That is the table as it stood before the update and before the delete.

Query
SELECT SALE_ID, REGION, AMOUNT
FROM PAST_T134
ORDER BY SALE_ID;
Result6 rows · 3 columns
SALE_IDREGIONAMOUNT
1north100.00
2south250.50
3east75.25
4west310.00
5north45.00
6south88.25

All six rows, with the two south amounts back at 250.50 and 88.25.

This is not a query against history. It is a real table, built from history, that you can read, join and write to.

An offset works as well, if you want a moment rather than a statement.

Query
CREATE OR REPLACE TABLE PASTOFF_T134 CLONE SALES_T134 AT (OFFSET => -5);
Result1 row · 1 column
status
Table PASTOFF_T134 successfully created.
Query
SELECT COUNT(*)    AS ROWS_IN_IT,
       SUM(AMOUNT) AS TOTAL_AMOUNT
FROM PASTOFF_T134;
Result1 row · 2 columns
ROWS_IN_ITTOTAL_AMOUNT
4485.25

Four rows and 485.25, which is a few seconds ago rather than the beginning.

And the two ways of asking for a moment that is not there

First, a moment before the table existed at all.

Query
CREATE OR REPLACE TABLE PASTNEVER_T134 CLONE SALES_T134
BEFORE (STATEMENT => $QID_START);
Query failed — Snowflake returned an error
000707 (02000): Time travel data is not available for table SALES_T134. The requested time is either beyond the allowed time travel period or before the object creation time.

000707, time travel data is not available. The message names both reasons at once: either beyond the allowed period, or before the object was created.

And second, a positive offset, which asks for the future.

Query
CREATE OR REPLACE TABLE PASTFUTURE_T134 CLONE SALES_T134 AT (OFFSET => 60);
Query failed — Snowflake returned an error
002038 (22000): SQL compilation error:
Invalid data type [60] in AT(OFFSET => 60)

002038, Invalid data type [60] in AT(OFFSET => 60). There is nothing wrong with the data type. The number is simply not allowed to be positive, and the message does not say so.

Now let's clone a whole schema

A schema is where cloning stops being simple, because the things inside a schema refer to each other.

So let's build one with two views over the same table. The first names its table plainly. The second spells out the whole path. Their bodies are otherwise identical.

A schema to clone
-- A schema with things inside it that refer to each other.
CREATE OR REPLACE SCHEMA CLONEZONE_T134;

CREATE OR REPLACE TABLE CLONEZONE_T134.INSIDE_T134 (
    SALE_ID INT,
    AMOUNT  NUMBER(10,2)
);

INSERT INTO CLONEZONE_T134.INSIDE_T134 (SALE_ID, AMOUNT)
VALUES (1, 100.00), (2, 200.00);

-- The first view names its table plainly.
CREATE OR REPLACE VIEW CLONEZONE_T134.PLAIN_VIEW_T134 AS
SELECT COUNT(*)    AS ROWS_IT_CAN_SEE,
       SUM(AMOUNT) AS TOTAL_AMOUNT
FROM INSIDE_T134;

-- The second one spells out the whole path. Same body otherwise.
CREATE OR REPLACE VIEW CLONEZONE_T134.QUALIFIED_VIEW_T134 AS
SELECT COUNT(*)    AS ROWS_IT_CAN_SEE,
       SUM(AMOUNT) AS TOTAL_AMOUNT
FROM DEMO_DB.CLONEZONE_T134.INSIDE_T134;

-- And one more table, cloned across the schema line from PUBLIC.
CREATE OR REPLACE TABLE CLONEZONE_T134.OVER_THERE_T134 CLONE SALES_CLONE_T134;
What the cloned-in table holds
SELECT COUNT(*)    AS ROWS_IN_IT,
       SUM(AMOUNT) AS TOTAL_AMOUNT
FROM CLONEZONE_T134.OVER_THERE_T134;
Result1 row · 2 columns
ROWS_IN_ITTOTAL_AMOUNT
71369.00

Seven rows and 1369.00, so cloning across a schema line works the same as cloning inside one.

Now let's clone the whole schema in one statement.

Query
CREATE OR REPLACE SCHEMA CLONEZONE_CLONE_T134 CLONE CLONEZONE_T134;
Result1 row · 1 column
status
Schema CLONEZONE_CLONE_T134 successfully created.
Query
SELECT TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE, ROW_COUNT
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA IN ('CLONEZONE_T134', 'CLONEZONE_CLONE_T134')
ORDER BY TABLE_SCHEMA, TABLE_NAME;
Result8 rows · 4 columns
TABLE_SCHEMATABLE_NAMETABLE_TYPEROW_COUNT
CLONEZONE_CLONE_T134INSIDE_T134BASE TABLE2
CLONEZONE_CLONE_T134OVER_THERE_T134BASE TABLE7
CLONEZONE_CLONE_T134PLAIN_VIEW_T134VIEWNULL
CLONEZONE_CLONE_T134QUALIFIED_VIEW_T134VIEWNULL
CLONEZONE_T134INSIDE_T134BASE TABLE2
CLONEZONE_T134OVER_THERE_T134BASE TABLE7
CLONEZONE_T134PLAIN_VIEW_T134VIEWNULL
CLONEZONE_T134QUALIFIED_VIEW_T134VIEWNULL

Eight rows, four on each side. Both tables and both views came across. The views have no ROW_COUNT, and those cells are real NULLs rather than zeroes.

And what the cloned table holds
SELECT COUNT(*)    AS ROWS_IN_IT,
       SUM(AMOUNT) AS TOTAL_AMOUNT
FROM CLONEZONE_CLONE_T134.INSIDE_T134;
Result1 row · 2 columns
ROWS_IN_ITTOTAL_AMOUNT
2300.00

Two rows and 300.00, the same as the original.

Now let's change the original only

One row into the original table, and nothing into the clone.

Query
INSERT INTO CLONEZONE_T134.INSIDE_T134 (SALE_ID, AMOUNT) VALUES (3, 300.00);
Result1 row · 1 column
number of rows inserted
1
The original
SELECT COUNT(*) AS ROWS_IN_THE_ORIGINAL FROM CLONEZONE_T134.INSIDE_T134;
Result1 row · 1 column
ROWS_IN_THE_ORIGINAL
3
The clone
SELECT COUNT(*) AS ROWS_IN_THE_CLONE FROM CLONEZONE_CLONE_T134.INSIDE_T134;
Result1 row · 1 column
ROWS_IN_THE_CLONE
2

Three rows on one side, two on the other. Now we can ask each view, inside the clone, what it can see.

The unqualified view, in the clone
SELECT ROWS_IT_CAN_SEE, TOTAL_AMOUNT
FROM CLONEZONE_CLONE_T134.PLAIN_VIEW_T134;
Result1 row · 2 columns
ROWS_IT_CAN_SEETOTAL_AMOUNT
2300.00

Two rows and 300.00. That is its own table, in its own schema. Which is what you would expect.

The qualified view, in the clone
SELECT ROWS_IT_CAN_SEE, TOTAL_AMOUNT
FROM CLONEZONE_CLONE_T134.QUALIFIED_VIEW_T134;
Result1 row · 2 columns
ROWS_IT_CAN_SEETOTAL_AMOUNT
3600.00

Three rows and 600.00.

That is the original's number. The qualified view is sitting inside the cloned schema and reading the original table, across the schema line, in a schema it was never cloned into.

And the catalog shows the mechanism rather than an explanation of it.

Query
SELECT TABLE_NAME, VIEW_DEFINITION
FROM INFORMATION_SCHEMA.VIEWS
WHERE TABLE_SCHEMA = 'CLONEZONE_CLONE_T134'
ORDER BY TABLE_NAME;
Result2 rows · 2 columns
TABLE_NAMEVIEW_DEFINITION
PLAIN_VIEW_T134CREATE OR REPLACE VIEW CLONEZONE_CLONE_T134.PLAIN_VIEW_T134 AS SELECT COUNT(*) AS ROWS_IT_CAN_SEE, SUM(AMOUNT) AS TOTAL_AMOUNT FROM INSIDE_T134
QUALIFIED_VIEW_T134CREATE OR REPLACE VIEW CLONEZONE_CLONE_T134.QUALIFIED_VIEW_T134 AS SELECT COUNT(*) AS ROWS_IT_CAN_SEE, SUM(AMOUNT) AS TOTAL_AMOUNT FROM DEMO_DB.CLONEZONE_T134.INSIDE_T134

Both definitions now say CLONEZONE_CLONE_T134 at the top, because both views really are in the clone. Look at the FROM line of each. One reads FROM INSIDE_T134. The other still reads FROM DEMO_DB.CLONEZONE_T134.INSIDE_T134.

This is the one to remember

Cloning re-points an unqualified name and leaves a qualified one exactly where it was pointing. So two views with the same body, written two different ways, end up reading two different tables. If you clone a schema to get a safe copy to work in, every fully qualified reference inside it is still aimed at production.

And did the stage come with it?

There was a stage in that schema too, with one file on it. Let's look at what we put there before the clone.

Unloading one file
COPY INTO @CLONEZONE_T134.STG_T134/out/
FROM (SELECT SALE_ID, AMOUNT FROM CLONEZONE_T134.INSIDE_T134)
FILE_FORMAT = (TYPE = CSV)
OVERWRITE = TRUE;
Result1 row · 3 columns
rows_unloadedinput_bytesoutput_bytes
21833

Two rows unloaded, and this grid reports output_bytes of 33.

What is on the stage
LIST @CLONEZONE_T134.STG_T134;
Result1 row · 4 columns
namesizemd5last_modified
stg_t134/out/data_0_0_0.csv.gz48a757ad74e4c2ca54c5c9b3430a12a403Fri, 2 Oct 2026 15:45:50 GMT

One file, and this grid reports its size as 48. Those are two different measurements taken by two different statements, so take each number from the grid it came from and do not expect them to agree.

Now let's list the same stage inside the cloned schema.

Query
LIST @CLONEZONE_CLONE_T134.STG_T134;
Query failed — Snowflake returned an error
002003 (02000): SQL compilation error:
Stage 'DEMO_DB.CLONEZONE_CLONE_T134.STG_T134' does not exist or not authorized. Your primary role ACCOUNTADMIN or one of your secondary roles must have at least one privilege granted on STAGE DEMO_DB.CLONEZONE_CLONE_T134.STG_T134.

002003, the stage does not exist. The schema cloned, the tables cloned, the views cloned, and the stage did not come at all.

One statement clones a whole database, and moves you into it

Before we clone anything, let's look at where the session is pointing.

Query
SELECT CURRENT_DATABASE() AS THE_DATABASE,
       CURRENT_SCHEMA()   AS THE_SCHEMA;
Result1 row · 2 columns
THE_DATABASETHE_SCHEMA
DEMO_DBCLONEZONE_CLONE_T134

DEMO_DB, and the schema we created a few sections ago. Creating a schema had already moved the session into it, which is easy not to notice.

Now let's clone the database.

Query
CREATE OR REPLACE DATABASE CLONEDB_T134 CLONE DEMO_DB;
Result1 row · 1 column
status
Database CLONEDB_T134 successfully created.
Query
SELECT CURRENT_DATABASE() AS THE_DATABASE,
       CURRENT_SCHEMA()   AS THE_SCHEMA;
Result1 row · 2 columns
THE_DATABASETHE_SCHEMA
CLONEDB_T134PUBLIC

CLONEDB_T134 and PUBLIC. Creating a database moves your session into it.

Which means an unqualified name now means something else entirely.

Query
SELECT COUNT(*)    AS ROWS_AN_UNQUALIFIED_NAME_FINDS,
       SUM(AMOUNT) AS TOTAL_AMOUNT
FROM SALES_CLONE_T134;
Result1 row · 2 columns
ROWS_AN_UNQUALIFIED_NAME_FINDSTOTAL_AMOUNT
71369.00

Seven rows and 1369.00, resolved inside the clone. The same statement would have found the same numbers in DEMO_DB on this data, which is exactly why it is dangerous.

So let's put the session back where it belongs before going any further.

Query
USE DATABASE DEMO_DB;
Result1 row · 1 column
status
Statement executed successfully.
Query
SELECT CURRENT_DATABASE() AS THE_DATABASE,
       CURRENT_SCHEMA()   AS THE_SCHEMA;
Result1 row · 2 columns
THE_DATABASETHE_SCHEMA
DEMO_DBPUBLIC

Now we can read the clone by name instead of by accident.

The original
SELECT COUNT(*) AS TABLES_IN_DEMO_DB_PUBLIC
FROM DEMO_DB.INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'PUBLIC';
Result1 row · 1 column
TABLES_IN_DEMO_DB_PUBLIC
18
The clone
SELECT COUNT(*) AS TABLES_IN_THE_CLONED_DB_PUBLIC
FROM CLONEDB_T134.INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'PUBLIC';
Result1 row · 1 column
TABLES_IN_THE_CLONED_DB_PUBLIC
18

Eighteen tables in PUBLIC on each side.

Query
SELECT SCHEMA_NAME
FROM CLONEDB_T134.INFORMATION_SCHEMA.SCHEMATA
ORDER BY SCHEMA_NAME;
Result4 rows · 1 column
SCHEMA_NAME
CLONEZONE_CLONE_T134
CLONEZONE_T134
INFORMATION_SCHEMA
PUBLIC

Four schemas, including both of the ones we made earlier. The schemas came too.

Query
SELECT COUNT(*)    AS ROWS_IN_IT,
       SUM(AMOUNT) AS TOTAL_AMOUNT
FROM CLONEDB_T134.PUBLIC.SALES_CLONE_T134;
Result1 row · 2 columns
ROWS_IN_ITTOTAL_AMOUNT
71369.00
Cleanup code runs in the wreckage

This is the part that cost a whole run. The teardown at the end of the demo script dropped CLONEDB_T134 using a bare name — and that was the database the session was sitting in, because creating it had moved the session there. Every cleanup statement after that was refused for having no current database, and the objects were left on the account.

That refusal is not one of the grids on this page. The script was fixed before this run, so there is no capture of it, and inventing an error pane for it would make this post exactly the kind of thing it is trying not to be. The teardown here puts the session somewhere it knows first and then uses fully qualified names, which you can see in the last section.

A few more things CLONE will not do

Four refusals, and each one tells you something about where the clause stops.

A source that is not there.

Query
CREATE OR REPLACE TABLE NOSOURCE_T134 CLONE NOSUCHTABLE_T134;
Query failed — Snowflake returned an error
002003 (42S02): SQL compilation error:
Object 'NOSUCHTABLE_T134' does not exist or not authorized. Your primary role ACCOUNTADMIN or one of your secondary roles must have at least one privilege granted on TABLE NOSUCHTABLE_T134.

A target name that is already taken, written without OR REPLACE.

Query
CREATE TABLE SALES_CLONE2_T134 CLONE SALES_T134;
Query failed — Snowflake returned an error
002002 (42710): SQL compilation error:
Object 'SALES_CLONE2_T134' already exists.

002002, already exists. Now a view. Can a view be cloned?

Query
CREATE OR REPLACE VIEW SALESVIEW_CLONE_T134 CLONE SALESVIEW_T134;
Query failed — Snowflake returned an error
001003 (42000): SQL compilation error:
syntax error line 1 at position 50 unexpected 'SALESVIEW_T134'.
syntax error line 1 at position 64 unexpected '<EOF>'.

001003, a syntax error. CREATE VIEW … CLONE is not a form the parser knows at all, which is a different kind of no from the next one.

Query
CREATE OR REPLACE TABLE FROMVIEW_T134 CLONE SALESVIEW_T134;
Query failed — Snowflake returned an error
002118 (0A000): SQL compilation error: Cannot clone from a view object.

002118, Cannot clone from a view object. That statement parses, and then the clause refuses the source.

And a schema that does not exist.

Query
CREATE OR REPLACE SCHEMA NOSCHEMA_T134 CLONE NOSUCHSCHEMA_T134;
Query failed — Snowflake returned an error
002003 (02000): SQL compilation error:
Schema 'DEMO_DB.NOSUCHSCHEMA_T134' does not exist or not authorized. Your primary role ACCOUNTADMIN or one of your secondary roles must have USAGE or any other privilege granted on SCHEMA DEMO_DB.NOSUCHSCHEMA_T134.

One last thing: the pattern that matched nothing

The SHOW TABLES query above used LIKE '%\_T134', with a backslash in front of the underscore, and it matched both tables. Let's ask the catalog view the same thing four different ways.

How many there really are
SELECT COUNT(*) AS TABLES_ENDING_IN_T134
FROM DEMO_DB.INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'PUBLIC'
  AND ENDSWITH(TABLE_NAME, '_T134');
Result1 row · 1 column
TABLES_ENDING_IN_T134
2
Four ways of asking
SELECT COUNT_IF(ENDSWITH(TABLE_NAME, '_T134'))      AS BY_ENDSWITH,
       COUNT_IF(TABLE_NAME LIKE '%\\_T134')          AS BY_BACKSLASH_LIKE,
       COUNT_IF(TABLE_NAME LIKE '%_T134')           AS BY_PLAIN_LIKE,
       COUNT_IF(TABLE_NAME LIKE '%\\_T134' ESCAPE '\\') AS BY_LIKE_WITH_ESCAPE
FROM DEMO_DB.INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'PUBLIC';
Result1 row · 4 columns
BY_ENDSWITHBY_BACKSLASH_LIKEBY_PLAIN_LIKEBY_LIKE_WITH_ESCAPE
2022

ENDSWITH finds two. A plain LIKE '%_T134' finds two. The same pattern with an ESCAPE clause finds two. And the backslash pattern on its own finds none.

Let's look at what that pattern actually is.

Query
SELECT '%\\_T134'            AS THE_PATTERN_AS_SNOWFLAKE_SEES_IT,
       LENGTH('%\\_T134')     AS HOW_MANY_CHARACTERS,
       'SALES_T134' LIKE '%\\_T134'                 AS DOES_IT_MATCH,
       'SALES_T134' LIKE '%\\_T134' ESCAPE '\\'      AS WITH_AN_ESCAPE_CLAUSE,
       'SALES_T134' LIKE '%_T134'                  AS PLAINLY;
Result1 row · 5 columns
THE_PATTERN_AS_SNOWFLAKE_SEES_ITHOW_MANY_CHARACTERSDOES_IT_MATCHWITH_AN_ESCAPE_CLAUSEPLAINLY
%\_T1347FALSETRUETRUE

Seven characters, and the backslash is one of them. Without an ESCAPE clause, LIKE is looking for a literal backslash in the table name, and there isn't one.

So a pattern that works in SHOW TABLES LIKE can find nothing in a WHERE clause, with no error either way. If you are counting your own objects, use ENDSWITH and sleep better.

And the cleanup, written for the wreckage

Every statement in the teardown names its database and schema in full, and the session is put somewhere it knows before any of them run. Then we check.

Where we are
SELECT CURRENT_DATABASE() AS THE_DATABASE,
       CURRENT_SCHEMA()   AS THE_SCHEMA;
Result1 row · 2 columns
THE_DATABASETHE_SCHEMA
DEMO_DBPUBLIC
Tables left
SELECT COUNT(*) AS PROBE_TABLES_LEFT
FROM DEMO_DB.INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'PUBLIC'
  AND ENDSWITH(TABLE_NAME, '_T134');
Result1 row · 1 column
PROBE_TABLES_LEFT
0
Views left
SELECT COUNT(*) AS PROBE_VIEWS_LEFT
FROM DEMO_DB.INFORMATION_SCHEMA.VIEWS
WHERE TABLE_SCHEMA = 'PUBLIC'
  AND ENDSWITH(TABLE_NAME, '_T134');
Result1 row · 1 column
PROBE_VIEWS_LEFT
0
Schemas left
SELECT COUNT(*) AS PROBE_SCHEMAS_LEFT
FROM DEMO_DB.INFORMATION_SCHEMA.SCHEMATA
WHERE ENDSWITH(SCHEMA_NAME, '_T134');
Result1 row · 1 column
PROBE_SCHEMAS_LEFT
0
Databases left
SHOW DATABASES LIKE '%T134';
Result0 rows · 14 columns · scroll →
created_onnameis_defaultis_currentoriginownercommentoptionsretention_timekindowner_role_typeobject_visibilitydata_quality_monitoring_settingsresharing_settings

Nothing left, and the session is back in DEMO_DB.PUBLIC.

Key points

  • A clone is a second name over data that is already stored. SHOW TABLES reports 1024 bytes for the source and 1024 for the clone, for data stored once.
  • No column anywhere in that listing says a table is a clone.
  • The clustering key travels. automatic_clustering does not — it is ON for the source and OFF for the clone.
  • Write to either side and the other does not move.
  • A clone outlives its source being dropped, because the storage was never the source's to own.
  • Retention is copied at clone time. The clone made before the change kept 1 day, the one made after it got 3.
  • Transient cannot be cloned to permanent (002120). The other direction is accepted.
  • CLONE … BEFORE (STATEMENT => …) gives you a real table built from the past, not a query against it.
  • Cloning a schema re-points an unqualified reference and leaves a qualified one aimed at the original. The two views in the clone read 2 and 3 rows.
  • A cloned schema does not bring its stages.
  • CREATE DATABASE … CLONE moves your session into the new database, which changes what every unqualified name means.
  • So write a teardown that puts the session somewhere it knows and then names everything in full.
Next in this series

Episode 135 — streams. A bookmark into a table's change tracking, the three metadata columns a stream read gives you, and a stream that says it has data and then returns zero rows.

Every query and every result grid on this page was captured from a live Snowflake account (version 10.35.101) running the demo script for this episode. One grid is deliberately left out: the one that prints the statement id held in a session variable, because a query id belongs to one account's own history. The teardown failure described in the database section happened on an earlier run and was not captured, and no error pane is shown for it. Every count in the text is read off one of the grids on this page. Nothing is timed and no performance claims are made anywhere in this series. Behaviour follows the Snowflake CREATE … CLONE documentation.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.