Forum Stats

  • 3,759,448 Users
  • 2,251,545 Discussions
  • 7,870,645 Comments

Discussions

Issues with procedure!

user782973-Oracle
user782973-Oracle Member Posts: 67
edited Jan 20, 2015 3:53AM in SQL & PL/SQL

Hi, Can someone help me understand whats wrong with my procedure.

CREATE OR REPLACE procedure VALID_PROC( START_DATE IN DATE, END_DATE IN DATE)
IS


cursor c1 is 
SELECT * FROM EMP
WHERE HIREDATE BETWEEN TO_DATE('01-01-85','DD-MM-YY') AND TO_DATE('01-01-80','DD-MM-YY');
c_row c1%rowtype;
BEGIN
OPEN C1;
fetch c1 into c_row;
if c1%notfound then 
endif;


INSERT INTO emp_back(empno,ename,job,mgr,hiredate,sal,comm,deptno)
VALUES(c_row.empno,c_row.ename,c_row.job,c_row.hiredate,c_row.sal,c_row.comm,c_row.deptno);


commit;
close c1;
END;
/
Tagged:
Boneist
«1

Answers

  • Frank Kulash
    Frank Kulash Member, Moderator Posts: 41,068 Red Diamond
    edited Jan 19, 2015 9:56AM

    Hi,

    That depends on what "wrong" means to you.  What is the procedure supposed to do?  What is it doing differently?

    Whenever you have a question, please post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all the tables involved, and the exact results you want from that data, so that the people who want to help you can re-create the problem and test their ideas.  (If you're using the Oracle-supplied scott.emp table, you don't need to post CREATE TABLE and INSERT statements for it; just make it clear what you're doing.)

    If you're asking about a DML operation, such as UPDATE, then the INSERT statements you post should show what the tables are like before the DML, and the results will be the contents of the changed table after the DML.

    Explain, using specific examples, how you get those results from that data.

    Always say what version of Oracle you're using (e.g. 11.2.0.2.0).

    See the forum FAQ: 

    You're using 2-digit years; that's always a mistake,

  • ascheffer
    ascheffer Member Posts: 1,911 Gold Trophy

    Try to spot the difference with this:

    CREATE OR REPLACE procedure VALID_PROC( START_DATE IN DATE, END_DATE IN DATE)

    is

    begin

      insert into emp_back(empno,ename,job,mgr,hiredate,sal,comm,deptno)

      select c_row.empno,c_row.ename,c_row.job,c_row.hiredate,c_row.sal,c_row.comm,c_row.deptno

      from emp c_row

      where hiredate between start_date and end_date;

    end;

    It might give you a clue to what's wrong with your procedure

  • John Stegeman
    John Stegeman Member Posts: 24,269 Blue Diamond
    edited Jan 19, 2015 9:55AM

    ooh - exclamation point in the title, must be important.

    First, before I even read your procedure - why do you think there is something wrong with it? Did you get an error? If so, why didn't you tell us?

    Secondly, there are lot's of problems...

    1). Why are you using 2 digits to represent a year?

    2). You just blindly skip over the case if there is no record found

    3). You don't use the parameters you passed in

    4). Committing inside a stored procedure is often not the right thing to do

    5). Using PL/SQL when you didn't need to

    6). Not processing all of the rows in the cursor (assuming that was your intent)

    There may be more, but those jumped right to mind

  • user782973-Oracle
    user782973-Oracle Member Posts: 67
    edited Jan 19, 2015 10:03AM

    Hi John, I do get a lot of errors. First i tried using dynamic values which didn't work and hence tried static dates.

    CREATE OR REPLACE procedure VALID_PROC( START_DATE IN DATE, END_DATE IN DATE)
    IS
    cursor c1 is 
    SELECT * FROM EMP
    WHERE HIREDATE BETWEEN TO_DATE(START_DATE,'DD-MM-YY') AND TO_DATE(END_DATE,'DD-MM-YY');
    c_row c1%rowtype;
    BEGIN
    OPEN C1;
    fetch c1 into c_row;
    if c1%notfound then 
    endif;
    INSERT INTO emp_back(empno,ename,job,mgr,hiredate,sal,comm,deptno)
    VALUES(c_row.empno,c_row.ename,c_row.job,c_row.hiredate,c_row.sal,c_row.comm,c_row.deptno);
    commit;
    close c1;
    END;
    /
    

    And i tried

    select TO_DATE('01-01-80','DD-MM-YY') from dual;
    

    which gives me date. So not sure what you meant by "2 digits to represent a year".

  • Hi Frank, My apologies, will surely follow instructions. I was trying to create a procedure which inserts values based on 2 dates. I'm using oracle scott.emp data.

  • user782973-Oracle
    user782973-Oracle Member Posts: 67
    edited Jan 19, 2015 10:08AM

    Thanks Ascheffer! That helps. However the validation fails and still gives errors when i try and complile.

  • ascheffer
    ascheffer Member Posts: 1,911 Gold Trophy
    edited Jan 19, 2015 10:12AM
    • select TO_DATE('01-01-80','DD-MM-YY') from dual; 

    will give a date in 2080. Probably not what you want

  • Frank Kulash
    Frank Kulash Member, Moderator Posts: 41,068 Red Diamond

    Hi,

    user782973-Oracle wrote:
    
    Thanks Ascheffer! That helps. However the validation fails and still gives errors when i try and complile.
    

    Don't you think it would be helpful to say what the errors are?

  • ascheffer
    ascheffer Member Posts: 1,911 Gold Trophy
    edited Jan 19, 2015 10:36AM

    No need, I'm very good at guessing:

    endif should be end if;

    PLS-00103: Encountered the symbol ";" when expecting one of the following:

       if
    06550. 00000 -  "line %s, column %s:\n%s"
    *Cause:    Usually a PL/SQL compilation error.
    *Action:

    And if you fixed that you wil get

    PLS-00103: Encountered the symbol "END" when expecting one of the following:

       begin case declare exit for goto if loop mod null pragma
       raise return select update while with <een ID>
       <een scheidingsteken-ID tussen dubbele aanhalingstekens>
       <een bindvariabele> << close current delete fetch lock insert
       open rollback savepoint set sql execute commit forall merge
       pipe
    06550. 00000 -  "line %s, column %s:\n%s"
    *Cause:    Usually a PL/SQL compilation error.
    *Action:

    because you forgot to put a statement after your if clause:

  • Frank Kulash
    Frank Kulash Member, Moderator Posts: 41,068 Red Diamond
    edited Jan 19, 2015 10:47AM
    user782973-Oracle wrote:
    
    ...
    And i tried
    
    
    
    
    1. select TO_DATE('01-01-80','DD-MM-YY') from dual; 
    select TO_DATE('01-01-80','DD-MM-YY') from dual;
    
    which gives me date. So not sure what you meant by "2 digits to represent a year".
    
    
    
    

    It means using a string like '01-01-80', where only 2 digits (e.g. '8' and '0') are supposed to indicate the year.

    The trouble with 2-digit years is that you can easily get confused by 2 different years (in different centuries) having the same last 2 digits.  For example, the table might have data from 1980, but your procedure might be looking for data from 2080.

This discussion has been closed.