在表中插入,Sequence.nextval不起作用

时间:2013-11-21 10:58:42

标签: sql oracle11g oracle-sqldeveloper sql-insert

我有以下3个表格,

Data_Excel 包含人名,地址,城市和来源;
人员表有姓名和ID;
我需要在 person_location 中插入地址来源,地址,城市和ID ......

我使用以下查询:

CREATE SEQUENCE seq
START WITH 6571
MINVALUE 6571
INCREMENT BY 1
CACHE 100

INSERT INTO Person (id,Name,source) 
Select (seq.nextval),p_name,source 
FROM Data_Excel 
WHERE P_Name NOT IN 
(SELECT name FROM Person) 
GROUP BY P_Name,P_Address,P_city,Source 
HAVING count(*) < 2;

但是我收到以下错误。 我正在使用seq,因为ID是人的主键,但它不是自动递增的。我也试过,但有一个错误:

02287. 00000 -  "sequence number not allowed here" 
*Cause:    The specified sequence umber (CURRVAL or NEXTVAL) is inappropriate 
here in the statement. 
*Action:    emove the sequence number. 

I have the following 3 tables, Data_Excel contains the names, address, city and source of person; Person table has the name and ID; I need to insert into person_location the address source, address, city and ID...where ID comes from person table and name that exist against the id should be matched in the data_excel table to get all the details

1 个答案:

答案 0 :(得分:7)

尝试将序列移出分组查询:

INSERT INTO Person (id,Name,source) 
SELECT seq.nextval, p_name,source FROM (
Select p_name,source 
FROM Data_Excel 
WHERE P_Name NOT IN 
(SELECT name FROM Person) 
GROUP BY P_Name,P_Address,P_city,Source 
HAVING count(*) < 2
);