Skip to main content

Changing Table Data with SQL

SQL data manipulation (DML) statements change the rows of an existing Magpie table in place, without rebuilding it: INSERT adds rows, UPDATE changes them, DELETE removes them, and MERGE does all three by matching the table against a source.

DML statements are SQL. Run them in a SQL paragraph, or in a Magpie paragraph wrapped in sql(...), as the examples below do. Unlike a query, a DML statement in sql(...) runs immediately, without a show. The examples assume an existing Delta table of orders, sales.orders, and staging tables that feed it.

Which tables you can change​

Only the table being changed is restricted; the query that supplies the rows can read any table you can read.

Target tableINSERT INTOINSERT OVERWRITEUPDATE, DELETE, MERGE, INSERT … REPLACE WHERE
Delta filesYesYesYes
Other file formats: Parquet, ORC, CSV, JSON, Avro, textYesYesNo
Writable connector-backed tables: JDBC, Postgres, Redshift, Snowflake, BigQuery, GenericYesNoNo
Kafka tables, tables defined by a SQL query, read-only tablesNoNoNo

The table must be in your current repository, and you need Write permission on it and Read permission on every table the statement reads.

Adding new rows​

To add the latest data to a table without rewriting what's already there, use INSERT INTO. Setting the load date as a variable lets the same paragraph run for any day:

set load_date = '2026-07-21' as date;

sql(
insert into sales.orders
select order_id, customer_id, amount, status, order_date
from staging.new_orders
where order_date = $load_date
);

Columns are written by position, so list them in the table's order. INSERT INTO doesn't check for duplicates: running it twice adds the rows twice. To insert from one source into several tables at once, see Multi-Table Insert.

See the full documentation for details: Insert Into

Correcting rows​

To fix bad values or remove rows that shouldn't be there, use UPDATE and DELETE:

sql(update sales.orders set amount = 31.00 where order_id = 1004);

sql(delete from sales.orders where order_id = 1002);

Without a WHERE clause, UPDATE changes every row and DELETE removes every row.

See the full documentation for details: Update, Delete

Keeping a table in sync with a source​

When changes arrive as a feed, with some rows new and some updating rows you already have, MERGE applies the whole feed in one statement:

sql(
merge into sales.orders t
using staging.order_updates s
on t.order_id = s.order_id
when matched then update set *
when not matched then insert *
);

Orders already in the table are updated from the feed, new ones are inserted, and orders the feed doesn't mention are left alone. Unlike INSERT INTO, running the same merge twice changes nothing the second time. If the source is a complete snapshot, adding when not matched by source then delete also removes rows the snapshot no longer contains.

See the full documentation for details: Merge Into

Refreshing a table on a schedule​

To keep a summary table up to date, recompute only the rows that can have changed. INSERT … REPLACE WHERE replaces the rows that match a condition in one transaction, so readers never see a half-rebuilt table. Run it nightly from a job as a Magpie script task:

alter job refresh_daily_sales add magpie script task {
"name": "refresh_recent_days"
} with script """
sql(
insert into analytics.daily_sales
replace where sale_date >= date_sub(current_date(), 3)
select order_date, count(*), sum(amount)
from sales.orders
where order_date >= date_sub(current_date(), 3)
group by order_date
);
""";

alter job refresh_daily_sales add schedule {
"name": "nightly",
"expression": "0 0 2 * * ?",
"schemaName": "analytics"
};

The query should produce only rows that match the replace where condition, so the same condition appears in both. To rebuild the whole table instead, use insert overwrite analytics.daily_sales select ….

Don't use a SQL mapper task for DML: it saves its query's result to a target table, so it applies the change and then fails, and a retry applies the change again.

See the full documentation for details: Insert Replace Where, Insert Overwrite, Creating Jobs

A shorter example: a Parquet table​

Not every table is Delta. This example shows what you can and can't do with a table stored as Parquet. Create a small table of page visits, with "format": "parquet":

sql(
select 1 as visit_id, '/index' as page
union all
select 2, '/pricing'
);

save result as table {
"name": "page_visits",
"persistenceMapping": {
"mappingType": "FileSystem",
"sourceName": "my_s3",
"file": { "format": "parquet", "path": "/web", "fileName": "page_visits" }
}
} in schema sales with data source my_s3;

Appending works on any file format, so Insert Into succeeds:

sql(insert into sales.page_visits values (3, '/docs'));

show 10 from sql(select * from sales.page_visits order by visit_id);

Result:

visit_idpage
1/index
2/pricing
3/docs

Changing an existing row does not. The old home page path should read /home:

sql(update sales.page_visits set page = '/home' where page = '/index');

Magpie refuses the statement before touching the table:

UPDATE is only supported on Delta tables for file-backed targets; "page_visits" uses Parquet.

DELETE, MERGE, and INSERT … REPLACE WHERE are refused the same way. If you need to change or remove rows, store the table as Delta, like sales.orders above.

What DML doesn't do​

Troubleshooting​

Magpie checks the target before reading any source, so a refused statement changes nothing. In the messages below, <statement> is the statement name and <table> the target table.

MessageCause and fix
INSERT source produces <n> columns, but target "<table>" expects <m> data columns …The query returns the wrong number of columns. List exactly the table's columns, in order.
<statement> is only supported on Delta tables for file-backed targets; "<table>" uses <format>.UPDATE, DELETE, MERGE, and REPLACE WHERE need a Delta table.
<statement> is not supported for table "<table>" (connector: <type>). … row-level DML requires a Delta table.The table is connector-backed. Only INSERT INTO works on it.
INSERT OVERWRITE is not supported for table "<table>" (connector: <type>). …Overwriting a connector-backed table isn't allowed.
<statement> does not support a repository-qualified target …The target names a repository. Run use repository and name the table without it.
DML target "<table>" is marked read-only.The table's persistence mapping isn't writable.
<statement> is not supported for table "<table>": tables with SQL persistence are views and cannot be written.The table is defined by a SQL query. Change the tables it reads instead.
<statement> is not supported for table "<table>" (connector: Kafka). …Kafka tables are append-only streams.
DML is not supported on table "<table>" because it has a non-identity field mapping.A field of the table maps to a file column with a different name.
DML target '<table>' cannot be used as a column-reference qualifier …The statement gives the table an alias but qualifies a column with the table name, or an insert's query qualifies a column with the target's name. Use the alias.
Mutating SQL statements are not supported with "<command>". Execute the statement with sql(...).DML was passed to a read-only command such as show … from sql(...).
DML target "<table>" has no persistence mapping.The table has no persistence mapping, so there is nothing to write to.
<statement> is not supported for table "<table>": database query persistence mappings are not writable.The table maps to a database query rather than a database table. BigQuery SQL-expression mappings have an equivalent message.
Unable to write to table since persistence mapping path contains wildcards.The table's file path contains a wildcard.
Error processing SQL statement: …Any other failure, with Spark's message after the colon.
Unable to create table <table>: Datasource does not support writing empty or nested empty schemas…DML ran in a SQL mapper task: the change was applied, then saving its empty result failed. Use a Magpie script task.

For the full syntax and every error each statement can return, see the DML section of the Magpie SQL command reference.