Solved

Get one Sequence Value for entire statment

Posted on 2013-05-15
6
425 Views
Last Modified: 2013-05-15
Afternoon Experts - I have a question on doing an insert with a sequence.

As an example, I have a table of users, table for test grades and a table to house my hiring class.  

Applicant Table:
AP__ID
AP_NAME
AP_IsHired

Civil Service Grades:
CS_ID,
CS_APID (fk over to AP_ID)
CS_GRADE,
CS_TEST_DATE,
CS_EXPIRE_DATE

HIRING CLASS:
HC_ID
HC_APID (fk to AP_ID)
HC_STARTDATE,
HC_ENDDATE
HC_CLASS

I wrote a quick sequence to get the next available hiring class

CREATE SEQUENCE S_HIRINGCLASS
  MINVALUE 1
  MAXVALUE 99999
  START WITH 1
  INCREMENT BY 1
  CACHE 20;

When I run this it "works" But it enters each person with a new Sequence


INSERT INTO HIRINGCLASS (HC_AP_ID, hc_start_date, HC_CLASS)
SELECT AP_ID, SYSDATE, S_HIRINGCLASS.NEXTVAL
FROM APPLICANT, CIVILSERVICE
WHERE NOT EXISTS
(SELECT 1
FROM HIRINGCLASS
WHERE APPLICANT.AP_ID = HIRINGCLASS.HC_AP_ID)
AND CS_APPID = AP_ID
AND AP_ISHIRED = 'N'
AND ROUND (F_MONTH_DIFF (SYSDATE, CS_EXP_DATE)) > 3;

Each time I run this I want to get ALL applicants that qualify (AP_ISHIRED is "N" and there are more than 3 months till the expire date on the scores) and assign them all to ONE hiring class, then the next time I run this statement it would pick the nextval from my sequence and assign them to the following class, etc, etc.?
0
Comment
Question by:hej613
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 39169372
Select S_HIRINGCLASS.NEXTVAL outside the insert and use

S_HIRINGCLASS.CURRVAL inside the insert.
0
 
LVL 1

Author Comment

by:hej613
ID: 39169379
That sounds simple - but how would I select it outside of the insert?

forgive the very basic question..

Thanks
0
 
LVL 41

Expert Comment

by:Sharath
ID: 39169400
I think you don't need a sequence here. All you need the max(HC_ID)+1 from the table for every load.
I would suggest to create HC_ID as an integer in your table and assin value like this. See if this helps.
INSERT INTO HIRINGCLASS (HC_ID,HC_AP_ID, hc_start_date, HC_CLASS)
SELECT (select coalesce(max(HC_ID),0)+1 from HIRINGCLASS) HC_ID
      AP_ID, SYSDATE, S_HIRINGCLASS.NEXTVAL
FROM APPLICANT, CIVILSERVICE
WHERE NOT EXISTS
(SELECT 1
FROM HIRINGCLASS
WHERE APPLICANT.AP_ID = HIRINGCLASS.HC_AP_ID)
AND CS_APPID = AP_ID
AND AP_ISHIRED = 'N'
AND ROUND (F_MONTH_DIFF (SYSDATE, CS_EXP_DATE)) > 3;

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 41

Accepted Solution

by:
Sharath earned 400 total points
ID: 39169414
corrected this if you need.
INSERT INTO HIRINGCLASS (HC_AP_ID, hc_start_date, HC_CLASS)
SELECT AP_ID, SYSDATE,(select coalesce(max(HC_CLASS),0)+1 from HIRINGCLASS) HC_CLASS
FROM APPLICANT, CIVILSERVICE
WHERE NOT EXISTS
(SELECT 1
FROM HIRINGCLASS
WHERE APPLICANT.AP_ID = HIRINGCLASS.HC_AP_ID)
AND CS_APPID = AP_ID
AND AP_ISHIRED = 'N'
AND ROUND (F_MONTH_DIFF (SYSDATE, CS_EXP_DATE)) > 3;

Open in new window

0
 
LVL 77

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 100 total points
ID: 39169512
>>That sounds simple - but how would I select it outside of the insert?

select S_HIRINGCLASS.NEXTVAL from dual;

INSERT INTO HIRINGCLASS (HC_AP_ID, hc_start_date, HC_CLASS)
SELECT AP_ID, SYSDATE, S_HIRINGCLASS.CURRVAL
FROM APPLICANT, CIVILSERVICE
...
0
 
LVL 1

Author Closing Comment

by:hej613
ID: 39169522
Great Solutions - Both.. Thank you.
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question