sql_query_insert {dbplyr} | R Documentation |
These functions generate the SQL used in rows_*(in_place = TRUE)
.
sql_query_insert(
con,
x_name,
y,
by,
...,
conflict = c("error", "ignore"),
returning_cols = NULL,
method = NULL
)
sql_query_append(con, x_name, y, ..., returning_cols = NULL)
sql_query_update_from(
con,
x_name,
y,
by,
update_values,
...,
returning_cols = NULL
)
sql_query_upsert(
con,
x_name,
y,
by,
update_cols,
...,
returning_cols = NULL,
method = NULL
)
sql_query_delete(con, x_name, y, by, ..., returning_cols = NULL)
con |
Database connection. |
x_name |
Name of the table to update. |
y |
A lazy tbl. |
by |
An unnamed character vector giving the key columns. The key columns
must exist in both By default, we use the first column in |
... |
Other parameters passed onto methods. |
conflict |
For One of:
|
returning_cols |
Optional. Names of columns to return. |
method |
Optional. The method to use. |
update_values |
A named SQL vector that specify how to update the columns. |
update_cols |
Names of columns to update. |
Insert Methods
"where_not_exists"
The default for most databases.
INSERT INTO x_name SELECT * FROM y WHERE NOT EXISTS <match on by columns>
"on_conflict"
Supported by:
Postgres
SQLite
This method uses the ON CONFLICT
clause and therefore requires a unique
index on the columns specified in by
.
Upsert Methods
"merge"
The upsert method according to the SQL standard. It uses the MERGE
statement
MERGE INTO x_name USING y ON <match on by columns> WHEN MATCHED THEN UPDATE SET ... WHEN NOT MATCHED THEN INSERT ...
"on_conflict"
Supported by:
Postgres
SQLite
This method uses the ON CONFLICT
clause and therefore requires a unique
index on the columns specified in by
.
"cte_update"
Supported by:
Postgres
SQLite
Oracle
The classical way to upsert in Postgres and SQLite before support for
ON CONFLICT
was added. The update is done in a CTE clause and the unmatched
values are then inserted outside of the CTE.
A SQL query.
lf <- lazy_frame(
carrier = c("9E", "AA"),
name = c("Endeavor Air Inc.", "American Airlines Inc."),
con = simulate_postgres()
)
sql_query_upsert(
simulate_postgres(),
ident("airlines"),
lf,
by = "carrier",
update_cols = "name"
)