Table Values Constructor in Oracle Database 23ai/26ai
The table values constructor allows us to define multiple rows using a single constructor for use in SQL statements.
oracle 23configurationintermediate
by OracleDba
15 views
The table values constructor allows us to define multiple rows using a single constructor for use in SQL statements.
12345678
drop table if exists t1;
create table t1 (
id number,
code varchar2(6),
description varchar(25),
constraint t1_pk primary key (id)
);1234567891011121314151617181920212223242526272829303132333435363738
insert into t1
values (1, 'ONE', 'Description for ONE'),
(2, 'TWO', 'Description for TWO'),
(3, 'THREE', 'Description for THREE');
commit;
select * from t1;
ID CODE DESCRIPTION
---------- ------ -------------------------
1 ONE Description for ONE
2 TWO Description for TWO
3 THREE Description for THREE
SQL>
insert into t1
values (4, 'FOUR', 'Description for ONE'),
(5, 'FIVE', 'This one will fail because it is too big'),
(6, 'SIX', 'Description for THREE');
insert into t1
*
ERROR at line 1:
ORA-12899: value too large for column "TESTUSER1"."T1"."DESCRIPTION" (actual: 40, maximum: 25)
select * from t1;
ID CODE DESCRIPTION
---------- ------ -------------------------
1 ONE Description for ONE
2 TWO Description for TWO
3 THREE Description for THREE
SQL>1234567891011121314
select *
from (values
(4, 'FOUR', 'Description for FOUR'),
(5, 'FIVE', 'Description for FIVE'),
(6, 'SIX', 'Description for SIX')
) a (id, code, description);
ID CODE DESCRIPTION
---------- ---- --------------------
4 FOUR Description for FOUR
5 FIVE Description for FIVE
6 SIX Description for SIX
SQL>1234567891011121314
with a (id, code, description) AS (
values (7, 'SEVEN', 'Description for SEVEN'),
(8, 'EIGHT', 'Description for EIGHT'),
(9, 'NINE', 'Description for NINE')
)
select * from a;
ID CODE DESCRIPTION
---------- ----- ---------------------
7 SEVEN Description for SEVEN
8 EIGHT Description for EIGHT
9 NINE Description for NINE
SQL>123456789101112131415161718192021222324252627282930313233343536
merge into t1 a
using (values
(4, 'FOUR', 'Description for FOUR'),
(5, 'FIVE', 'Description for FIVE'),
(6, 'SIX', 'Description for SIX')
) b (id, code, description)
on (a.id = b.id)
when matched then
update set a.code = b.code,
a.description = b.description
when not matched then
insert (a.id, a.code, a.description)
values (b.id, b.code, b.description);
3 rows merged.
SQL>
select * from t1;
ID CODE DESCRIPTION
---------- ------ -------------------------
1 ONE Description for ONE
2 TWO Description for TWO
3 THREE Description for THREE
4 FOUR Description for FOUR
5 FIVE Description for FIVE
6 SIX Description for SIX
6 rows selected.
SQL>
rollback;Please to add comments
No comments yet. Be the first to comment!