2013-11-21 33 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