那里:
我是两张桌子a)" incidents_all"和"事件"和一个序列" incident_ids" .ii)table,事件有一个额外的列id作为主键约束,而不是table,incidents_all。
iii)" incidents_all"包含重复记录。
目标:将唯一事件记录插入表格,"事件"来自表格," incidents_all"以及序列号。
我正在尝试插入唯一记录,如下所示:
a)使用以下方法在需要唯一性的列上创建唯一索引:
CREATE UNIQUE INDEX "evnt_time_hst_flt_code_idx" ON "incidents"
(
"host_name",
"down_since",
"fault_code"
);
b)填充"事件"通过从" ids"中选择序列号来获得具有唯一记录的表以及来自" incidents_all"。
的记录INSERT INTO "incidents"
(
"id",
"sl_no",
"host_name"
"down_since",
"category",
"fault_code"
)
SELECT
"incident_ids".NEXTVAL AS "id",
"incidents_all"."sl_no" AS "sl_no",
"incidents_all"."host_name" AS "host_name",
"incidents_all"."down_since" AS "down_since",
"incidents_all"."category" AS "category",
"incidents_all"."fault_code" AS "fault_code"
FROM "incidents_all", "incident_ids";
但是,获得Oracle例外,"序列不允许在这里!"。
请引导我插入唯一记录和序列号!
答案 0 :(得分:0)
试试这个:
INSERT INTO "incidents"
(
"id",
"sl_no",
"host_name"
"down_since",
"category",
"fault_code"
)
SELECT
"ids".NEXTVAL AS "id",
"incidents_all"."sl_no" AS "sl_no",
"incidents_all"."host_name" AS "host_name",
"incidents_all"."down_since" AS "down_since",
"incidents_all"."category" AS "category",
"incidents_all"."fault_code" AS "fault_code"
FROM "incidents_all";
您不需要在from子句中包含序列。
答案 1 :(得分:0)
试试这个:
INSERT INTO "incidents"
(
"id",
"sl_no",
"host_name"
"down_since",
"category",
"fault_code"
)
SELECT
incident_ids.NEXTVAL AS "id", -- ->!!!!
"incidents_all"."sl_no" AS "sl_no",
"incidents_all"."host_name" AS "host_name",
"incidents_all"."down_since" AS "down_since",
"incidents_all"."category" AS "category",
"incidents_all"."fault_code" AS "fault_code"
FROM "incidents_all";
如果需要序列的下一个值,则必须使用sequence_name.nextval。
祝你好运。答案 2 :(得分:0)
假设您有一个具有重复值的表,基于列a, b
和另一个具有相同结构的表,您希望在其中插入非重复值。
create table duplicateTable(id number, a number, b number, c number);
create table uniqueTable ( id number primary key, a number, b number, c number);
假设您要使用序列作为第二个表的id
create sequence id_seq start with 1;
假设数据如下
insert into duplicateTable(id, a, b, c) values (1, 100, 50, 1000);
insert into duplicateTable(id, a, b, c) values (2, 100, 50, 2000);
insert into duplicateTable(id, a, b, c) values (3, 100, 99, 9000);
insert into duplicateTable(id, a, b, c) values (4, 999, 50, 7777);
insert into duplicateTable(id, a, b, c) values (5, 999, 99, 0);
您必须决定如何处理不属于您的唯一键的列;例如,假设您希望避免基于a, b
上的键的重复项,并且您希望为给定的一对c
获得(a, b)
的最大值,则可以使用:< / p>
insert into uniqueTable ( id, a, b, c)
select id_seq.nextVal, a, b, c
from (
select a, b, max(c) as c
from duplicateTable
group by a, b
)
SQL> select * from uniqueTable;
ID A B C
---------- ---------- ---------- ----------
1 999 99 0
2 100 50 2000
3 100 99 9000
4 999 50 7777
答案 3 :(得分:0)
参考,http://www.dba-oracle.com/t_packages_dbms_errlog.htm和Bhavesh Ghodasara的回答,
a)创建错误记录表:
CREATE TABLE err$_log_table
(
ora_err_number$ NUMBER,
ora_err_mesg$ VARCHAR2(2000),
ora_err_rowid$ rowid,
ora_err_optyp$ VARCHAR2(2),
ora_err_tag$ VARCHAR2(2000)
);
b)填充&#34;事件&#34;来自&#34; incidents_all&#34;的记录表并从&#34; ids&#34;中选择序列号:
INSERT INTO "incidents"
(
"id",
"sl_no",
"host_name"
"down_since",
"category",
"fault_code"
)
SELECT
"incident_ids".NEXTVAL AS "id",
"incidents_all"."sl_no" AS "sl_no",
"incidents_all"."host_name" AS "host_name",
"incidents_all"."down_since" AS "down_since",
"incidents_all"."category" AS "category",
"incidents_all"."fault_code" AS "fault_code"
FROM "incidents_all"
LOG ERRORS INTO err$_log_table ('duplicate') REJECT LIMIT UNLIMITED;
为表&#34;事件添加了独特的记录&#34;并在遇到重复记录时记录表中的错误,错误$ _log_table。