如何在Oracle中的视图上使用AUTO_INCREMENT创建id

时间:2016-10-27 15:59:58

标签: oracle oracle11g

任何人都可以帮助在oracle 11g中的视图上创建AUTO_INCREMENT列。

由于

1 个答案:

答案 0 :(得分:0)

虽然无法为其基础数据没有任何单个唯一标识符的视图返回单个唯一标识列,但可以返回唯一标识数据的复合值。例如,给定一个CSV数据表,每行有一个唯一的ID:

create table sample (id number primary key, csv varchar2(4000));

其中CSV列包含一串逗号分隔值:

insert into sample 
select 1, 'a' from dual union all
select 2, 'b,c' from dual union all
select 3, 'd,"e",f' from dual union all
select 4, ',h,' from dual union all
select 5, 'j,"",l' from dual union all
select 6, 'm,,o' from dual;

以下查询将取消忽略csv数据,并且复合值(IDSEQ)将唯一标识每个VAL ue,ID列标识记录数据来自,SEQ唯一标识CSV中的位置:

WITH pvt(id, seq, csv, val, nxt) as (
SELECT id  -- Parse out individual list items
     , 1   -- separated by commas and 
     , csv -- optionally enclosed by quotes
     , REGEXP_SUBSTR(csv,'(["]?)([^,]*)\1',1,1,null,2)
     , REGEXP_INSTR(csv, ',', 1, 1)
FROM sample
UNION ALL
SELECT id
     , seq+1
     , csv
     , REGEXP_SUBSTR(csv,'(["]?)([^,]*)\1',nxt+1,1,null,2)
     , REGEXP_INSTR(csv, ',', nxt+1, 1)
FROM pvt
 where nxt > 0
)
select * from pvt order by id, seq;

        ID        SEQ CSV        VAL               NXT
---------- ---------- ---------- ---------- ----------
         1          1 a          a                   0

         2          1 b,c        b                   2
         2          2 b,c        c                   0

         3          1 d,"e",f    d                   2
         3          2 d,"e",f    e                   6
         3          3 d,"e",f    f                   0

         4          1 ,h,        [NULL]              1
         4          2 ,h,        h                   3
         4          3 ,h,        [NULL]              0

         5          1 j,"",l     j                   2
         5          2 j,"",l     [NULL]              5
         5          3 j,"",l     l                   0

         6          1 m,,o       m                   2
         6          2 m,,o       [NULL]              3
         6          3 m,,o       o                   0


15 rows selected.