是否可以在 Oracle 的 FROM 子句中使用 CASE WHEN 表达式?
Is it possible to use a CASE WHEN expression in a FROM clause in Oracle?
上下文
使用 Oracle 12c 标准版(其许可模型不允许使用 Table 分区)。
目标
在触发器中使用它来将行动态分派到不同的表。
例子
CREATE OR REPLACE trigger TRG_DISPATCH
instead of insert or update on MY_VIEW
for each row
DECLARE
partition_id SIMPLE_INTEGER ;
BEGIN
partition_id := 1 ;
insert into
case
when partition_id = 1 then "my_table_1"
when partition_id = 2 then "my_table_2"
end ( "id", "code", "msg" )
values
( :new."id", :new."code", :new."msg" )
;
END ;
/
备注
我 已经 知道 this cannot be achieved using Microsoft SQL Server 2005 但并非所有的 RDBMS 都是平等的。
首先,为什么不直接使用 Oracle 中的内置分区功能?容易多了。
Oracle 支持 insert first
:
insert first
when partition_id = 1 then
into my_table_1( "id", "code", "msg" )
values ( :new."id", :new."code", :new."msg" )
when partition_id = 2 then
into my_table_2( "id", "code", "msg" )
values ( :new."id", :new."code", :new."msg" );
上下文
使用 Oracle 12c 标准版(其许可模型不允许使用 Table 分区)。
目标
在触发器中使用它来将行动态分派到不同的表。
例子
CREATE OR REPLACE trigger TRG_DISPATCH
instead of insert or update on MY_VIEW
for each row
DECLARE
partition_id SIMPLE_INTEGER ;
BEGIN
partition_id := 1 ;
insert into
case
when partition_id = 1 then "my_table_1"
when partition_id = 2 then "my_table_2"
end ( "id", "code", "msg" )
values
( :new."id", :new."code", :new."msg" )
;
END ;
/
备注 我 已经 知道 this cannot be achieved using Microsoft SQL Server 2005 但并非所有的 RDBMS 都是平等的。
首先,为什么不直接使用 Oracle 中的内置分区功能?容易多了。
Oracle 支持 insert first
:
insert first
when partition_id = 1 then
into my_table_1( "id", "code", "msg" )
values ( :new."id", :new."code", :new."msg" )
when partition_id = 2 then
into my_table_2( "id", "code", "msg" )
values ( :new."id", :new."code", :new."msg" );