2013-11-21 34 views
3

我有以下3个表,插入表,Sequence.nextval不工作

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

回答

6

尝试移动的顺序进行分组查询:

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 
); 
+0

它的工作原理谢谢,如果我要做到这一点一般喜欢无论何时我要插入一个值,我会像人表中的max(id)+1那样,我该怎么做? –

+0

一些事情是这样的: CREATE SEQUENCE序列与 START(选择最大(从人ID)+1) MINVALUE(选择MAX(ID)+1的人) 递增1个 CACHE 100 –

+0

我猜想(因为我没有Oracle在这里检查),而不是'SELECT seq.nextval,p_name,source FROM',你可以使用'SELECT ROWNUM +(SELECT MAX(id)FROM Person),p_name,source FROM'。如果它工作(尝试它),然后被警告,它被认为是不好的做法,因为:当由多个客户端在同一时间执行时,语句可能会得到相同的'MAX(id)'并产生重复键,导致重复键错误。 – halfbit