我想将列值从一个表复制到另一个表中,但操作类似(不起作用):
create sequence INSTITUTION_SEQ
minvalue 1
maxvalue 999999999999999999999
start with 1
increment by 1
cache 20;
INSERT INTO INSTITUTION
(ID, NAME)
VALUES(
INSTITUTION_SEQ.nextval,
SELECT DISTINCT ACB_BANK_NAME FROM LUP ORDER BY ACB_BANK_NAME
);
问题
DROP INSTITUTION_SEQ
但导致了ORA-00950: invalid DROP option
答案 0 :(得分:2)
您可以使用以下方法获取序列值,而不是创建/使用/删除序列:
INSERT INTO INSTITUTION (ID, NAME)
SELECT ROW_NUMBER() OVER (ORDER BY ACB_BANK_NAME), ACB_BANK_NAME
FROM
( -- DISTINCT is processed after ROW_NUMBER
SELECT DISTINCT ACB_BANK_NAME FROM LUP
) dt;
INSERT INTO INSTITUTION (ID, NAME)
SELECT ROW_NUMBER() OVER (ORDER BY ACB_BANK_NAME), ACB_BANK_NAME
FROM LUP
GROUP BY ACB_BANK_NAME; -- GROUP BY is processed before ROW_NUMBER
答案 1 :(得分:2)
您无需创建和删除序列,可以使用rownum
或分析函数:
INSERT INTO INSTITUTION (ID, NAME)
SELECT rownum, ACB_BANK_NAME
FROM (
SELECT DISTINCT ACB_BANK_NAME FROM LUP ORDER BY ACB_BANK_NAME
);