赞
踩
本次测试数据库版本Oracle 23ai Free Release 23.0.0.0.0
SYS@FREE> select * from v$version; BANNER -------------------------------------------------------------------------------- BANNER_FULL -------------------------------------------------------------------------------- BANNER_LEGACY -------------------------------------------------------------------------------- CON_ID ---------- Oracle Database 23ai Free Release 23.0.0.0.0 - Develop, Learn, and Run for Free Oracle Database 23ai Free Release 23.0.0.0.0 - Develop, Learn, and Run for Free Version 23.4.0.24.05 Oracle Database 23ai Free Release 23.0.0.0.0 - Develop, Learn, and Run for Free 0 BANNER -------------------------------------------------------------------------------- BANNER_FULL -------------------------------------------------------------------------------- BANNER_LEGACY -------------------------------------------------------------------------------- CON_ID ----------
Oracle23ai新特性,直接插入多行数据并不总是使用 INSERT ALL 语法。Oracle 提供了一种更简洁的方法来一次性插入多行数据,即使用单个 INSERT INTO 语句后跟多个 VALUES 子句。还可以使用values语句构建临时表。values语句构建数据和merge语句组合使用 。
在INSERT语句中,也可以实现多行的直接插入。
SYS@FREE> conn testuser/oracle@FREEPDB1 Connected. TESTUSER@FREEPDB1> create table t_multirows (id number,infoname varchar2(32)); Table created. TESTUSER@FREEPDB1> insert into t_multirows values(1,'oracle23c'),(2,'oracle23ai'),(3,'mysql8.4'); 3 rows created. TESTUSER@FREEPDB1> commit; Commit complete. TESTUSER@FREEPDB1> col infoname format a30 TESTUSER@FREEPDB1> select * from t_multirows; ID INFONAME ---------- ------------------------------ 1 oracle23c 2 oracle23ai 3 mysql8.4
TESTUSER@FREEPDB1> select * from (values(1,'multiple'),(2,'values')) T (id,nameinfo);
ID NAMEINFO
---------- ------------------------
1 multiple
2 values
cte (common table expressions)公用表表达式。
TESTUSER@FREEPDB1> with cte_values (id,nameinfo) as (values(1,'common'),(2,'table'),(3,'expressions')) select * from cte_values; ID NAMEINFO ---------- -------------------- 1 common 2 table 3 expressions -- Oracle19c新增的自增id特性 generated by default as identity CREATE TABLE t_identity ( id NUMBER generated by DEFAULT AS identity , nameinfo varchar2(32), sysguid varchar2(36), CONSTRAINT pk_t_identity_id PRIMARY KEY(id) ); -- sqlplus testuser/oracle@FREEPDB1 INSERT INTO t_identity(nameinfo,sysguid) values('generated by',sys_guid()),('DEFAULT',sys_guid()),('AS identity',sys_guid()); COMMIT; -- 查询数据表 TESTUSER@FREEPDB1> SELECT * FROM t_identity; ID NAMEINFO SYSGUID ---------- ------------------------------ ------------------------------------ 1 generated by 18EFA5E23DDC0BA1E063E650A8C05A76 2 DEFAULT 18EFA5E23DDD0BA1E063E650A8C05A76 3 AS identity 18EFA5E23DDE0BA1E063E650A8C05A76 -- 再次插入两行数据 INSERT INTO t_identity(nameinfo,sysguid) values('get sys_guid',sys_guid()),('䶮中国字',sys_guid()); COMMIT; TESTUSER@FREEPDB1> SELECT * FROM t_identity; ID NAMEINFO SYSGUID ---------- ------------------------------ ------------------------------------ 1 generated by 18EFA5E23DDC0BA1E063E650A8C05A76 2 DEFAULT 18EFA5E23DDD0BA1E063E650A8C05A76 3 AS identity 18EFA5E23DDE0BA1E063E650A8C05A76 4 get sys_guid 18EFA5E23DDF0BA1E063E650A8C05A76 5 䶮中国字 18EFA5E23DE00BA1E063E650A8C05A76
MERGE into t_identity t1 using (values(4,'values meger',sys_guid()),(7,'table',sys_guid()),(8,'expressions',sys_guid())) t2 (ID,nameinfo,sysguid) on (t2.id=t1.id) when matched then UPDATE set t1.nameinfo=t2.nameinfo,t1.sysguid=t2.sysguid when not matched then insert (t1.id,t1.nameinfo,t1.sysguid) values(t2.id,t2.nameinfo,t2.sysguid); TESTUSER@FREEPDB1> SELECT * FROM t_identity; ID NAMEINFO SYSGUID ---------- ------------------------------ ------------------------------------ 1 generated by 18EFA5E23DDC0BA1E063E650A8C05A76 2 DEFAULT 18EFA5E23DDD0BA1E063E650A8C05A76 3 AS identity 18EFA5E23DDE0BA1E063E650A8C05A76 4 values meger 18EFA5E23DE10BA1E063E650A8C05A76 5 䶮中国字 18EFA5E23DE00BA1E063E650A8C05A76 7 table 18EFA5E23DE20BA1E063E650A8C05A76 8 expressions 18EFA5E23DE30BA1E063E650A8C05A76 7 rows selected. -- 可以看到ID=4的NAMEINFO='values meger',MERGE into字句执行之前是'get sys_guid' -- 即ID关联匹配到的数据执行了更新操作,ID没有关联匹配到的数据执行了插入操作
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。