using case when ..need suggestion [message #647830] |
Tue, 09 February 2016 17:17 |
|
M123
Messages: 47 Registered: February 2016 Location: USA
|
Member |
|
|
CREATE OR REPLACE PROCEDURE as1(annual_sal dept.salary%TYPE, bonus dept.salary%TYPE);
BEGIN annual_sal := salary *12;
CASE WHEN annual_sal>=20000 THEN bonus := salary+2000;
WHEN annual_sal >=10000
AND <=19999 THEN bonus :=salary+1000;
WHEN annual_sal <= 9999 THEN bonus :=salary+500;
ELSE EXCEPTION WHEN CASE_NOT_FOUND THEN DBMS_OUTPUT.PUT_LINE('No increment');
END CASE;
END;
trying to use case when logic for creating bonus variable and updating salary based on annual salary and the department table dept has following variables.
Name Null Type
------ ---- ------------
JOB VARCHAR2(20)
JOBID VARCHAR2(20)
EMPID VARCHAR2(20)
SALARY NUMBER(21)
it shows error while compilation
Error(5,1): PLS-00103: Encountered the symbol "BEGIN"
Error(10,36): PLS-00103: Encountered the symbol "=" when expecting one of the following: ( - + case mod new not null <an identifier> <a double-quoted delimited-identifier> <a bind variable> continue avg count current exists max min prior sql stddev sum variance execute forall merge time timestamp interval date <a string literal with character set specification> <a number> <a single-quoted SQL string> pipe <an alternatively-quoted string literal with character set specification> <an alternatively
Error(13,5): PLS-00103: Encountered the symbol "EXCEPTION" when expecting one of the following: ( begin case declare exit for goto if loop mod null pragma raise return select update while with <an identifier> <a double-quoted delimited-identifier> <a bind variable> << continue close current delete fetch lock insert open rollback savepoint set sql execute commit forall merge pipe purge
Error(16,7): PLS-00103: Encountered the symbol "CASE" when expecting one of the following: ; <an identifier> <a double-quoted delimited-identifier> current delete exists prior <a single-quoted SQL string>
[Updated on: Tue, 09 February 2016 17:39] Report message to a moderator
|
|
|
|
|
|
Re: using case when ..need suggestion [message #647835 is a reply to message #647830] |
Tue, 09 February 2016 18:37 |
|
M123
Messages: 47 Registered: February 2016 Location: USA
|
Member |
|
|
declare
annual_sal number;
bonus number;
v_sal number;
begin
select salary into v_sal from dept;
annual_sal := v_sal *12;
case when
annual_sal>=20000 then bonus := v_sal+2000;
when annual_sal >=10000 and <=19999 then bonus :=v_sal+1000;
when annual_sal <= 9999 then bonus :=v_sal+500;
else
dbms_output.put_line('no incmrent');
end case;
else
end;
I certainly know somewhere i am missing / logic in declaration and passing the value ? need to try again
error:
Error report -
ORA-06550: line 11, column 36:
PLS-00103: Encountered the symbol "=" when expecting one of the following:
( - + case mod new not null <an identifier>
<a double-quoted delimited-identifier> <a bind variable>
continue avg count current exists max min prior sql stddev
sum variance execute forall merge time timestamp interval
date <a string literal with character set specification>
<a number> <a single-quoted SQL string> pipe
<an alternatively-quoted string literal with character set specification>
<an alternatively
ORA-06550: line 16, column 4:
PLS-00103: Encountered the symbol "ELSE" when expecting one of the following:
( begin case declare end exception exit for goto if loop mod
null pragma raise return select update while with
<an identifier> <a double-quoted delimi
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
[Updated on: Tue, 09 February 2016 18:56] Report message to a moderator
|
|
|
Re: using case when ..need suggestion [message #647837 is a reply to message #647835] |
Tue, 09 February 2016 18:56 |
Solomon Yakobson
Messages: 3273 Registered: January 2010 Location: Connecticut, USA
|
Senior Member |
|
|
I suggest you start reading PL/SQL documentation. SELECT INTO statement MUST select one and only one row. Parameter annual_sal is declared as IN mode parameter - it ca't be assigned value. Keep working...
SY.
|
|
|
|
|
|
|
Re: using case when ..need suggestion [message #647853 is a reply to message #647843] |
Wed, 10 February 2016 03:39 |
|
M123
Messages: 47 Registered: February 2016 Location: USA
|
Member |
|
|
declare
v_sal number;
annual_sal number;
bonus number;
begin
SELECT SALARY INTO v_sal from dept ;
annual_sal :=v_sal*12;
case when
annual_sal>=20000 then bonus := v_sal+2000;
when annual_sal >=10000 and <=19999 then bonus :=v_sal+1000;
when annual_sal <= 9999 then bonus :=v_sal+500;
else
dbms_output.put_line('no incmrent');
end case;
else
end;
missing some logic here ? can someone suggest .
need to update salary based on annualsal, annual_sal and bonus are new variables.
Error report -
ORA-06550: line 10, column 36:
PLS-00103: Encountered the symbol "=" when expecting one of the following:
( - + case mod new not null <an identifier>
<a double-quoted delimited-identifier> <a bind variable>
continue avg count current exists max min prior sql stddev
sum variance execute forall merge time timestamp interval
date <a string literal with character set specification>
<a number> <a single-quoted SQL string> pipe
<an alternatively-quoted string literal with character set specification>
<an alternatively
ORA-06550: line 15, column 4:
PLS-00103: Encountered the symbol "ELSE" when expecting one of the following:
( begin case declare end exception exit for goto if loop mod
null pragma raise return select update while with
<an identifier> <a double-quoted delimi
[Updated on: Wed, 10 February 2016 03:41] Report message to a moderator
|
|
|
|
|
Re: using case when ..need suggestion [message #647857 is a reply to message #647856] |
Wed, 10 February 2016 04:01 |
|
M123
Messages: 47 Registered: February 2016 Location: USA
|
Member |
|
|
create or replace procedure p1(emp_id in varchar2)is
v_sal number;
annual_sal number;
bonus number;
begin
SELECT SALARY INTO v_sal from dept where empid=emp_id;
annual_sal :=v_sal*12;
case when
annual_sal>=20000 then bonus := v_sal+2000;
when annual_sal >=10000 and annual_sal<=19999 then bonus :=v_sal+1000;
when annual_sal <= 9999 then bonus :=v_sal+500;
dbms_output.put_line(bonus);
else
dbms_output.put_line('no incmrent');
end case;
end;
/
comparison operator issue solved.but what should i do to apply this logic to all rows in a table ?
i used a procedure and executed like this:
execute p1(1) ;
here empid is 1, and in table it has salary of 2000
it shows plsql procedure completed successfully. but bonus is not displayed in end result ?????
[Updated on: Wed, 10 February 2016 04:19] Report message to a moderator
|
|
|
|
Re: using case when ..need suggestion [message #647859 is a reply to message #647857] |
Wed, 10 February 2016 04:26 |
cookiemonster
Messages: 13920 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
M123 wrote on Wed, 10 February 2016 10:01but what should i do to apply this logic to all rows in a table ?
Do you want to update all the rows in the table or display the bonus for all rows along with some other information?
If update - write a single update that uses case to set the appropriate column.
If display - write a single select that has case logic in the select clause to calculate bonus.
|
|
|
|
|
|
|
|
|
|
Re: using case when ..need suggestion [message #647885 is a reply to message #647873] |
Wed, 10 February 2016 16:30 |
|
Barbara Boehmer
Messages: 9090 Registered: November 2002 Location: California, USA
|
Senior Member |
|
|
One of the most important things that you need to do when posting a question here is explain what you are trying to accomplish. It appears that you want to update the salary for all rows of your dept table based on some bonus formula that is dependent upon the annual salary (salary * 12). If this is what you want to do, then you do not need a pl/sql procedure or a pl/sql block or any parameters. All you need is a sql update statement containing a case statement. If this is what you want to do, then you should focus on just a sql update statement containing a case statement and stop posting procedures and pl/sql blocks. If this is not what you want, then you should explain what you do want. Please see the following example that updates the sal column of the emp table using your bonus formula for all rows in the table. I have shown the values before and after the update. I have intentionally left part of the code blank. You need to figure out what goes in the blanks marked by three dots.
-- data before update:
SCOTT@orcl> select empno, ename, sal, sal*12 annual_sal from emp order by sal desc
2 /
EMPNO ENAME SAL ANNUAL_SAL
---------- ---------- ---------- ----------
7839 KING 5000 60000
7902 FORD 3000 36000
7788 SCOTT 3000 36000
7566 JONES 2975 35700
7698 BLAKE 2850 34200
7782 CLARK 2450 29400
7499 ALLEN 1600 19200
7844 TURNER 1500 18000
7934 MILLER 1300 15600
7521 WARD 1250 15000
7654 MARTIN 1250 15000
7876 ADAMS 1100 13200
7900 JAMES 950 11400
7369 SMITH 800 9600
14 rows selected.
-- SQL update statement with some of the code replaced by ...:
SCOTT@orcl> update emp
2 set sal =
3 case when ... then ...
4 when ... then ...
5 when ... then ...
6 else ...
7 end
8 /
14 rows updated.
-- data after update:
SCOTT@orcl> select empno, ename, sal from emp order by sal desc
2 /
EMPNO ENAME SAL
---------- ---------- ----------
7839 KING 7000
7902 FORD 5000
7788 SCOTT 5000
7566 JONES 4975
7698 BLAKE 4850
7782 CLARK 4450
7499 ALLEN 2600
7844 TURNER 2500
7934 MILLER 2300
7521 WARD 2250
7654 MARTIN 2250
7876 ADAMS 2100
7900 JAMES 1950
7369 SMITH 1300
14 rows selected.
|
|
|
|
|
|