- 3,722,538 Users
- 2,244,331 Discussions
- 7,849,911 Comments
Forum Stats
Discussions
Categories
- 16 Data
- 362.2K Big Data Appliance
- 7 Data Science
- 2K Databases
- 592 General Database Discussions
- 3.7K Java and JavaScript in the Database
- 32 Multilingual Engine
- 495 MySQL Community Space
- 7 NoSQL Database
- 7.7K Oracle Database Express Edition (XE)
- 2.8K ORDS, SODA & JSON in the Database
- 419 SQLcl
- 57 SQL Developer Data Modeler
- 185K SQL & PL/SQL
- 21.1K SQL Developer
- 2.3K Development
- 3 Developer Projects
- 32 Programming Languages
- 135.5K Development Tools
- 12 DevOps
- 3K QA/Testing
- 323 Java
- 10 Java Learning Subscription
- 12 Database Connectivity
- 70 Java Community Process
- 2 Java 25
- 11 Java APIs
- 141.2K Java Development Tools
- 8 Java EE (Java Enterprise Edition)
- 153K Java Essentials
- 135 Java 8 Questions
- 86.2K Java Programming
- 270 Java Lambda MOOC
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 14 Java SE
- 13.8K Java Security
- 3 Java User Groups
- 22 JavaScript - Nashorn
- 18 Programs
- 145 LiveLabs
- 34 Workshops
- 9 Software
- 3 Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 4 Deutsche Oracle Community
- 15 Español
- 1.9K Japanese
- 3 Portuguese
Issues with procedure!

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; /
Answers
-
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).
You're using 2-digit years; that's always a mistake,
-
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
-
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
-
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.
-
Thanks Ascheffer! That helps. However the validation fails and still gives errors when i try and complile.
-
- select TO_DATE('01-01-80','DD-MM-YY') from dual;
will give a date in 2080. Probably not what you want
-
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?
-
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:
-
user782973-Oracle wrote: ... And i tried
- 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.
-
Thanks Frank! Whats the right way of finding difference between two dates?
-
The right way is to use 4 digit year, use RR format or better use date literals. For example:
DATE '1980-01-01'
And BETWEEN works left to right, therefore start value must be less or equal to end value, therefore
WHERE HIREDATE BETWEEN TO_DATE('01-01-85','DD-MM-YY') AND TO_DATE('01-01-80','DD-MM-YY');
will never work. Use:WHERE HIREDATE BETWEEN DATE '1980-01-01' AND DATE '1985-01-01';
And keep in mind, the above includes January 1, 1985. Somehow I have a feeling you want:WHERE HIREDATE BETWEEN DATE '1980-01-01' AND DATE '1984-12-31';
SY. -
And one more thing. Why do you have literals in where clause? It sounds you should use procedure parameters:
WHERE HIREDATE BETWEEN START_DATE AND END_DATE;
SY.
-
There's nothing between line 12 and 13. You need to have some code between IF and END IF
if c1%notfound then
endif;
BTW why don't you do a simple INERT...SELECT ?
-
Hi,
This will solve your problem
------------------------------------
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-YYYY') AND TO_DATE(END_DATE,'DD-MM-YYYY');
c_row c1%rowtype;
BEGIN
OPEN C1;
loop
fetch c1 into c_row;
exit when c1%notfound;
INSERT INTO emp_back(empno,ename,job,mgr,hiredate,sal,comm,deptno)
VALUES(c_row.empno,c_row.ename,c_row.job,c_row.mgr,c_row.hiredate,c_row.sal,c_row.comm,c_row.deptno);
end loop;
close c1;
END;
--------------------------
mistakes : missed MGR value
didn't use loop
when Procedure/Function return compilation error...please try
SHOW ERROR PROCEDURE procedure_name;
it will give cause of error.
Thanks
Rajesh
-
user782973-Oracle wrote: 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;
- /
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; /
What's right with it is the question.... the answer being... "not much".
The whole thing can be simplified to just:
create or replace procedure valid_proc(start_date in date, end_date in date) as begin insert into emp_back(empno, ename, job, mgr, hiredate, sal, comm, deptno) select empno, ename, job, mgr, hiredate, sal, comm, deptno from emp where hiredate between start_date and end_date; commit; -- if appropriate to business/transaction logic end; /