Oracle Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesOracle Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 17th, 2005, 03:23 PM
suetonius suetonius is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 123 suetonius User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 5 h 49 m 16 sec
Reputation Power: 5
sequence in procedure (better question)

OK, here is a much better version of the procedure. The only problem here (I think) is that 'emp_no_seq.currVal' is not allowed in the UPDATE commands. This seems like an easier problem to solve. Anybody?

oracle8 Code:
Original - oracle8 Code
  1. (
  2. new_emp_last_name   employee.emp_last_name%TYPE,
  3. new_emp_first_name  employee.emp_first_name%TYPE,
  4. new_status          employee.status%TYPE,
  5. new_street       address.street%TYPE,
  6. new_city          address.city%TYPE,
  7. new_state        address.state%TYPE,
  8. new_zip          address.zip%TYPE
  9. )
  10. AS
  11.      
  12.       base_hour_wage NUMBER(1) :=6;
  13.       rowcount NUMBER(4) :=0;
  14.       CURSOR emp_insert IS
  15.                     SELECT emp_no
  16.                     FROM employee;
  17. BEGIN
  18.      SELECT COUNT(*) INTO rowcount
  19.      FROM employee
  20.      WHERE emp_no = emp_no_seq.NEXTVAL;;
  21.      
  22.      IF(rowcount = 0 AND new_status = 'tmp') THEN
  23.           INSERT INTO employee(emp_no, emp_last_name, emp_first_name, status)
  24.           VALUES(emp_no_seq.NEXTVAL, new_emp_last_name, new_emp_first_name, new_status);
  25.          
  26.           INSERT INTO temp(emp_no)
  27.           VALUES(emp_no_seq.CURRVAL);
  28.          
  29.           UPDATE temp SET base_wage = base_hour_wage
  30.           WHERE emp_no = emp_no_seq.CURRVAL;
  31.          
  32.           UPDATE temp SET summers_worked = summers_worked + 1     
  33.           WHERE emp_no = emp_no_seq.CURRVAL;
  34.          
  35.           UPDATE temp SET hourly = summers_worked + base_hour_wage
  36.           WHERE emp_no = emp_no_seq.CURRVAL;
  37.      
  38.      ELSE 
  39.              
  40.            IF(rowcount > 0)THEN       
  41.                 DBMS_OUTPUT.PUT_LINE('Employee number in use.');           
  42.            ELSE
  43.                 INSERT INTO employee(emp_no, emp_last_name, emp_first_name, status)
  44.                 VALUES(emp_no_seq.CURRVAL, new_emp_last_name, new_emp_first_name, new_status);       
  45.            END IF;
  46.        
  47.            IF(new_status = 'mgr')THEN
  48.            INSERT INTO manager(emp_no, salary)
  49.            VALUES(emp_no_seq.CURRVAL, 30000.00);
  50.            END IF;
  51.      
  52.            IF(new_status = 'asc')THEN
  53.            INSERT INTO ASSOCIATE(emp_no, salary)
  54.            VALUES(emp_no_seq.CURRVAL, 20000.00);
  55.            END IF;
  56.      END IF;
  57.  
  58.      INSERT INTO address VALUES(
  59.                  address_seq.NEXTVAL, new_street, new_city, new_state, new_zip);
  60.      INSERT INTO emp_address VALUES(
  61.                  address_seq.CURRVAL,emp_no_seq.CURRVAL);
  62.      
  63.      DBMS_OUTPUT.PUT_LINE('Employee added.');
  64. END;

Last edited by pabloj : November 18th, 2005 at 08:21 AM. Reason: Added proper syntax highlighting

Reply With Quote
  #2  
Old November 18th, 2005, 08:20 AM
pabloj's Avatar
pabloj pabloj is offline
Modding: Oracle MsSQL Firebird
Dev Shed God 7th Plane (8000 - 8499 posts)
 
Join Date: Jun 2001
Location: Outside US
Posts: 8,073 pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Months 4 Weeks 4 h 9 m 58 sec
Reputation Power: 281
currVal is already in use, you should go for nextVal, or get currVal from the one that got it.

Reply With Quote
  #3  
Old November 18th, 2005, 09:18 AM
suetonius suetonius is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 123 suetonius User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 5 h 49 m 16 sec
Reputation Power: 5
I don't understand what you mean. Is there some kind of self-referencing keyword like 'this.' in Oracle, as in Java/C++? It also occured to me that it might be best to use an exception to handle the possibility of a duplicate key value but my text does not cover exceptions at all. I've done a little research online but haven't come up with a solution yet. Also, the use of a sequence value in 'where' clauses is prohibited, so that doesn't work either. I would really like to be able to assign emp_no_seq.nextVal to a variable that I could then use throughout the procedure, but that doesn't seem to be allowed either. My text is really incomplete. Here is the latest version of the procedure. I think I have the exception written correctly but there is still the problem with the UPDATE commands.
PS. How'd you get the Oracle tags?

PHP Code:
(
new_emp_last_name   employee.emp_last_name%type,
new_emp_first_name  employee.emp_first_name%type,
new_status          employee.status%type,
new_street       address.street%type,
new_city          address.city%type,
new_state        address.state%type,
new_zip          address.zip%type
)
AS
      
      
base_hour_wage number(1) :=6
      
rowcount number(4) :=0;
      
CURSOR emp_insert IS
                    SELECT emp_no
                    FROM employee
;              
  
BEGIN       
                INSERT INTO employee
(emp_noemp_last_nameemp_first_namestatus
                
VALUES(emp_no_seq.nextValnew_emp_last_namenew_emp_first_namenew_status);        
           
       
               IF(
new_status 'mgr')THEN
                 INSERT INTO manager
(emp_nosalary)
                 
VALUES(emp_no_seq.currVal30000.00);
               
END IF;
       
               IF(
new_status 'asc')THEN
                 INSERT INTO associate
(emp_nosalary)
                 
VALUES(emp_no_seq.currVal20000.00);
               
END IF;

 IF( 
new_status 'tmp'THEN
          INSERT INTO employee
(emp_noemp_last_nameemp_first_namestatus
          
VALUES(emp_no_seq.currValnew_emp_last_namenew_emp_first_namenew_status);
          
          
INSERT INTO temp(emp_no)
          
VALUES(emp_no_seq.currVal);
          
          
UPDATE temp SET base_wage base_hour_wage
          WHERE emp_no 
emp_no_seq.currVal;
          
          
UPDATE temp SET summers_worked summers_worked 1     
          WHERE emp_no 
emp_no_seq.currVal;
          
          
UPDATE temp SET hourly summers_worked base_hour_wage
          WHERE emp_no 
emp_no_seq.currVal;
     
END IF; 

     
INSERT into address values(
                 
address_seq.nextValnew_streetnew_citynew_statenew_zip);
     
INSERT into emp_address values(
                 
address_seq.currVal,emp_no_seq.currVal);
     
     
DBMS_OUTPUT.PUT_LINE('Employee added.');

EXCEPTION
       WHEN DUP_VAL_ON_INDEX THEN
         DBMS_OUTPUT
.PUT_LINE('Employee number in use.');           

END

Reply With Quote
  #4  
Old November 23rd, 2005, 10:57 AM
jim mcnamara jim mcnamara is offline
......@.........
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2004
Posts: 1,308 jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 6 h 19 m 24 sec
Reputation Power: 48
He means once you got emp_no to be the next sequence number use the emp_no variable, don't use the sequence. Sequences cannot be depended on to have a constant value.
Comments on this post
pabloj agrees!

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesOracle Development > sequence in procedure (better question)


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT