91 lines
1.7 KiB
SQL
91 lines
1.7 KiB
SQL
# name: test/sql/upsert/upsert_aliased.test
|
|
# group: [upsert]
|
|
|
|
statement ok
|
|
pragma enable_verification;
|
|
|
|
statement ok
|
|
create table tbl (
|
|
i integer,
|
|
j integer unique
|
|
);
|
|
|
|
statement ok
|
|
insert into tbl values (5,3), (6,7);
|
|
|
|
# This works when we don't reference it anywhere
|
|
statement ok
|
|
insert into tbl AS test values (3,5), (8,3), (2,6) on conflict (j) do update set i = excluded.i * 2 where i <= excluded.i;
|
|
|
|
query II
|
|
select * from tbl order by all
|
|
----
|
|
2 6
|
|
3 5
|
|
6 7
|
|
16 3
|
|
|
|
# We can use the alias in the expressions
|
|
statement ok
|
|
insert into tbl AS test values (2,3) on conflict do update set i = test.i;
|
|
|
|
# Essentially a no-op
|
|
query II
|
|
select * from tbl order by all
|
|
----
|
|
2 6
|
|
3 5
|
|
6 7
|
|
16 3
|
|
|
|
# We can use the 'test' alias in the conflict target condition
|
|
|
|
query I
|
|
insert into tbl as test values (2,3) on conflict (j) do update set i = excluded.j where test.i < 5;
|
|
----
|
|
0
|
|
|
|
statement ok
|
|
insert into tbl as test values (2,3) on conflict (j) do update set i = excluded.j where test.i >= 5;
|
|
|
|
query II rowsort
|
|
select * from tbl
|
|
----
|
|
2 6
|
|
3 3
|
|
3 5
|
|
6 7
|
|
|
|
# We can also use the 'test' alias in the DO UPDATE condition
|
|
|
|
statement ok
|
|
insert into tbl as test (j, i) values (5,3) on conflict (j) do update set i = 10 where test.j <= 3;
|
|
|
|
# No-op, do update condition was not met
|
|
query II rowsort
|
|
select * from tbl;
|
|
----
|
|
2 6
|
|
3 3
|
|
3 5
|
|
6 7
|
|
|
|
statement ok
|
|
insert into tbl as test (j, i) values (5,3) on conflict (j) do update set i = 10 where test.j > 3;
|
|
|
|
# Now 'i' is changed to 10 in the conflicting tuple
|
|
query II
|
|
select * from tbl order by all
|
|
----
|
|
2 6
|
|
3 3
|
|
6 7
|
|
10 5
|
|
|
|
# When we alias to 'excluded' we create an ambiguity error
|
|
|
|
statement error
|
|
insert into tbl as excluded values (8,3) on conflict (j) do update set i = 5;
|
|
----
|
|
Ambiguous reference to table "excluded"
|