cursor for loop syntax issue [message #310954] |
Wed, 02 April 2008 23:43  |
horax
Messages: 34 Registered: March 2008
|
Member |
|
|
I have written the following code, and I know it's not great yet...I'm just trying to get the code to execute first, then I'll fine tune it for performance.
Anyway, let me show you the code, then I'll explain the error I'm receiving and hopefully you can help me understand why I'm receiving it.
DECLARE
CURSOR cur_sal IS
SELECT job, sal
FROM employee
FOR UPDATE NOWAIT;
lv_newsal employee.sal%type;
lv_raise_num NUMBER(6,2):=.06;
BEGIN
FOR rec_sal IN cur_sal LOOP
IF rec_sal.job='President' THEN lv_raise_num:=0 AND lv_newsal:=rec_sal.sal;
END IF;
IF rec_sal.job='Manager' THEN lv_newsal:=((rec_sal.sal*lv_raise_num)+rec_sal.sal);
END IF;
IF rec_sal.job='Salesman' THEN lv_newsal:=((rec_sal.sal*lv_raise_num)+rec_sal.sal);
END IF;
IF rec_sal.job='Clerk' THEN lv_newsal:=((rec_sal.sal*lv_raise_num)+rec_sal.sal);
END IF;
IF rec_sal.job='Analyst' THEN lv_newsal:=((rec_sal.sal*lv_raise_num)+rec_sal.sal);
END IF;
UPDATE employee
SET sal=lv_newsal
WHERE CURRENT OF cur_sal;
END LOOP;
END;
/
Errors:
IF rec_sal.job='President' THEN lv_raise_num:=0 AND lv_newsal=rec_sal.sal;
*
ERROR at line 11:
ORA-06550: line 11, column 50:
PLS-00382: expression is of wrong type
ORA-06550: line 11, column 36:
PL/SQL: Statement ignored
Why would it say that lv_raise_num:=0 is of wrong type? I've assigned it as a NUM...isn't 0 a num?
|
|
|
Re: cursor for loop syntax issue [message #310957 is a reply to message #310954] |
Wed, 02 April 2008 23:53   |
rajatratewal
Messages: 507 Registered: March 2008 Location: INDIA
|
Senior Member |
|
|
IF rec_sal.job='President' THEN lv_raise_num:=0 AND lv_newsal:=rec_sal.sal;
You are using and operation in b/w to statement
try this:-
IF rec_sal.job='President' THEN
lv_raise_num:=0 ;
lv_newsal:=rec_sal.sal;
|
|
|
|
|
Re: cursor for loop syntax issue [message #310961 is a reply to message #310959] |
Wed, 02 April 2008 23:59   |
horax
Messages: 34 Registered: March 2008
|
Member |
|
|
yeah, I saw that messed up := and fixed that right after posting.
As far as what I think it will do?
IF rec_sal.job='President' THEN lv_raise_num:=0 AND lv_newsal:=rec_sal.sal;
If the variable rec_sal.job returns the value of "president," then the raise will be equal to 0. His new salary will be assigned the value of his old salary.
|
|
|
Re: cursor for loop syntax issue [message #310965 is a reply to message #310961] |
Thu, 03 April 2008 00:01   |
horax
Messages: 34 Registered: March 2008
|
Member |
|
|
The first response helped! I replaced the AND operator with a simple ; to end the line and taht was all it took.
Does this mean that you cannot do AND oeprators in a cursor for loop?
|
|
|
Re: cursor for loop syntax issue [message #310967 is a reply to message #310954] |
Thu, 03 April 2008 00:02   |
flyboy
Messages: 1903 Registered: November 2006
|
Senior Member |
|
|
IF rec_sal.job='President' THEN lv_raise_num:=0 AND lv_newsal=rec_sal.sal;
Read that line again. What is the AND supposed to do there?
Yes, it is syntax error, AND is used in logical conditions, it is not valid as a part of the statement.
Just logical remark: you do not ORDER by query and change the value LV_RAISE_NUM inside the LOOP. As the result, not only employees with 'President' job, but all other employees updated after him, will use LV_RAISE_NUM = 0, not the initial .06 Seems as a bug, as this behaviour is not deterministic (for the same data, you may get different result).
> then I'll fine tune it for performance.
Best for performance would be throwing all the PL/SQL code away and UPDATE it in single SQL.
|
|
|
|
Re: cursor for loop syntax issue [message #310969 is a reply to message #310965] |
Thu, 03 April 2008 00:05   |
horax
Messages: 34 Registered: March 2008
|
Member |
|
|
DECLARE
CURSOR cur_sal IS
SELECT job, sal
FROM employee
FOR UPDATE NOWAIT;
lv_newsal employee.sal%type;
lv_raise_num NUMBER(6,2):=.06;
BEGIN
FOR rec_sal IN cur_sal LOOP
IF rec_sal.job='President' THEN lv_raise_num:=0;
lv_newsal:=rec_sal.sal;
ELSIF rec_sal.job='Manager' THEN lv_newsal:=((rec_sal.sal*lv_raise_num)+rec_sal.sal);
ELSIF rec_sal.job='Salesman' THEN lv_newsal:=((rec_sal.sal*lv_raise_num)+rec_sal.sal);
ELSIF rec_sal.job='Clerk' THEN lv_newsal:=((rec_sal.sal*lv_raise_num)+rec_sal.sal);
ELSIF rec_sal.job='Analyst' THEN lv_newsal:=((rec_sal.sal*lv_raise_num)+rec_sal.sal);
END IF;
UPDATE employee
SET sal=lv_newsal
WHERE CURRENT OF cur_sal;
END LOOP;
END;
/
I tried thsi instead to simplify it a bit, and I get the same output. Unforutnately, when I query the orignal table after the update, NOW all the salaries are=0 across teh board...doesnt' matter what their position is!
EDIT: I wrote this rsponse prior to seeing the latest from you guys. Thanks for your help! And please lay off the pseudo bashing, I'm new at PL/SQL...
[Updated on: Thu, 03 April 2008 00:07] Report message to a moderator
|
|
|
Re: cursor for loop syntax issue [message #310974 is a reply to message #310954] |
Thu, 03 April 2008 00:18   |
horax
Messages: 34 Registered: March 2008
|
Member |
|
|
Dangit...now, even AFTER rebuilding the table EMPLOYEE all the values change to 0 in the SAL column!
Here's the code I used:
DECLARE
CURSOR cur_sal IS
SELECT job, sal
FROM employee
FOR UPDATE NOWAIT;
lv_newsal employee.sal%type;
lv_raise_num NUMBER(6,2):=.06;
BEGIN
FOR rec_sal IN cur_sal LOOP
IF rec_sal.job='Analyst' THEN lv_newsal:=((rec_sal.sal*lv_raise_num)+rec_sal.sal);
ELSIF rec_sal.job='Manager' THEN lv_newsal:=((rec_sal.sal*lv_raise_num)+rec_sal.sal);
ELSIF rec_sal.job='Salesman' THEN lv_newsal:=((rec_sal.sal*lv_raise_num)+rec_sal.sal);
ELSIF rec_sal.job='Clerk' THEN lv_newsal:=((rec_sal.sal*lv_raise_num)+rec_sal.sal);
ELSIF rec_sal.job='President' THEN lv_newsal:=rec_sal.sal;
END IF;
UPDATE employee
SET sal=lv_newsal
WHERE CURRENT OF cur_sal;
END LOOP;
END;
/
Why woudl it do that?
|
|
|
|
Re: cursor for loop syntax issue [message #310984 is a reply to message #310954] |
Thu, 03 April 2008 03:01   |
flyboy
Messages: 1903 Registered: November 2006
|
Senior Member |
|
|
> Why woudl it do that?
It seems there are no employees with 'Analyst', 'Manager', 'Salesman', 'Clerk' nor 'President' job. Be aware, Oracle comparision is ordinarily case sensitive, so eg. 'Analyst' and 'ANALYST' are different strings. You either have to specify its case accurately or use one of LOWER, UPPER and INITCAP functions.
|
|
|
|
Re: cursor for loop syntax issue [message #311159 is a reply to message #310954] |
Thu, 03 April 2008 10:54  |
flyboy
Messages: 1903 Registered: November 2006
|
Senior Member |
|
|
> I need to show the total of all raises given (which I labeled lv_perc_num).
So, in other words, you want the SUM of lv_perc_num for all updated employees?
If so, you may sum it in the block into new variable. However this value will be available after update of all employees.
It does not seem reasonable to write it after each updated employee, as it would not change.
|
|
|