Module 06 · Lesson 2 of 10Detailed study · about 12 min

Preview, transform and keep file lineage

Query staged fields before loading; a COPY transformation can cast, reorder or select values and capture their source.

Understand the idea

This lesson uses base/orders.csv from the first lesson. A named format with SKIP_HEADER skips the header; it does not map CSV labels to columns.

Key ideas in simple English
ConceptWhat it means
$1, $2CSV fields by position.
METADATA$FILENAMESource path, useful for tracing a row.
METADATA$FILE_ROW_NUMBERRow position in the source file.
COPY SELECTSimple transformations during ingestion.

A concrete example

An audit row keeps order 1, amount 20 and the file path that supplied them.

Remember: Preview uses compute but does not insert rows. Complex joins and aggregations belong after ingestion.

Explain it step by step · full theory

How to apply it

This lesson uses base/orders.csv from the first lesson. A named format with SKIP_HEADER skips the header; it does not map CSV labels to columns.

Transformation COPY and name-based matching are separate loading approaches. Use strict casts when invalid values should fail; TRY_TO_NUMBER can turn bad input into NULL, so pair it with explicit quality checks.

Words used in this lesson
$1, $2
CSV fields by position.
METADATA$FILENAME
Source path, useful for tracing a row.
METADATA$FILE_ROW_NUMBER
Row position in the source file.
COPY SELECT
Simple transformations during ingestion.

See how it works

Choose a step to explore the idea at your own pace.

Open the interactive explanation
Interactive explainer1 / 3

Preview, transform and keep file lineage

$1, $2
METADATA$FILENAME

Understand the input

Query staged fields before loading; a COPY transformation can cast, reorder or select values and capture their source.

Automatic playback is off for reduced motion. Select any step or use Previous and Next. Illustrations explain behavior; they are not live measurements.

Read the complete diagram explanation
  1. Understand the input

    Query staged fields before loading; a COPY transformation can cast, reorder or select values and capture their source.

  2. Follow the example

    An audit row keeps order 1, amount 20 and the file path that supplied them.

  3. Check the outcome

    Preview uses compute but does not insert rows. Complex joins and aggregations belong after ingestion.

Try the SQL or command

Read → predict → run in your learning environment → compare. You can also learn by reading without an account.

New to SQL? Where to run it and how to read it

A query is a question written in SQL. Snowflake runs it and returns a result table. This website shows examples; it does not connect to your account.

  1. No account yet? Read and predict the output first. Follow the account and Snowsight lesson when you are ready.
  2. For SQL practice, use a learning account in Snowsight and select an authorized role and warehouse. A role determines permissions; a warehouse supplies processing power. Follow the example’s database/schema setup when it refers to stored objects.
  3. Read the entire example, including comments beginning with --. Run the selected statement or labeled block in the stated order. Examples labeled Python, shell or HTTP belong in that client, not in a SQL editor.
  4. Compare values and row counts with the explained result. If an example deliberately causes an error, follow its rollback and cleanup steps before continuing.
SELECT / FROM
Choose the values to return / identify their input.
VALUES / COLUMN1
VALUES supplies tiny example rows. COLUMN1 means their first column, COLUMN2 their second.
AS / WITH
AS gives a result a readable name. WITH names a temporary query step within one statement.
NULL / ::
NULL means missing or unknown. The :: notation converts a value to a named data type.
Session
One connection and its settings. Keep related transaction steps in the same session.
Database.schema.table
A full address for stored data. It is separate from the warehouse that processes queries.

Need the L2_OBJECTS_LAB setup? Open the first database-object lesson →

Help with common practice errors
  • No active warehouse: select a warehouse your role can use. Do not assume you need to create a new one.
  • Object missing or not authorized: check the full name, required setup and active role. Ask the account owner for access if needed.
  • Object already exists: setup may already have run. Read the current state or choose a fresh lab name consistently; do not add OR REPLACE blindly.
  • Different result: check whether an earlier INSERT or UPDATE changed the data. VALUES examples provide their own fresh input for each statement.

Before running this lesson

Manual examples only. Start with this module’s first lesson to create L2_OBJECTS_LAB.M6_LAB. In each new SQL session select that schema and an authorized warehouse. Later lessons state additional prerequisites. Upload file contents through Snowsight or a supported client; never run CSV as SQL.

Preview then load an audit table

SQL · example
SELECT $1, $2, METADATA$FILENAME, METADATA$FILE_ROW_NUMBER
FROM @M6_FILES/base/ (FILE_FORMAT => 'M6_CSV');
CREATE TABLE M6_AUDIT (ORDER_ID NUMBER, AMOUNT NUMBER(10,2), SOURCE_FILE VARCHAR, SOURCE_ROW NUMBER);
COPY INTO M6_AUDIT FROM (
 SELECT $1::NUMBER, $2::NUMBER(10,2), METADATA$FILENAME, METADATA$FILE_ROW_NUMBER
 FROM @M6_FILES/base/
) FILE_FORMAT=(FORMAT_NAME='M6_CSV');
SELECT * FROM M6_AUDIT ORDER BY ORDER_ID;

What to expect and why

The fresh audit table receives two records with file and row metadata. The actual path is supplied by Snowflake, not hard-coded.

Check your understanding

Choose an answer, then check your reasoning. This is practice, not a scored test.

Which field tells you the origin of a loaded row?

Try it yourself

How would you find all rows contributed by a suspect file?

Compare your reasoning

Filter SOURCE_FILE in the audit table; retain that value as part of ingestion lineage.

Official sources and curriculum

Module 06 · See module scope