Search code examples
sqloracleplsqldecode

Error: in oracle plsql of decode: PLS-00382: expression is of wrong type


Question: Your block should read in two real numbers and tell whether the product of the two numbers is equal to or greater than 100. Display the output on the screen using dbms_output.put_line. (Use decode instead of IF statement where required). Data has to be input by the user.

I am trying this on oracle plsql and getting stuck here.

Code:

CREATE OR REPLACE PROCEDURE two
AS
    i number;
    j number;
    p number;
    b boolean;
    msg varchar2(100);
BEGIN
    i:=&x;
    j:=&y;
    p:=i*j;
    b:=p>=100;
    select DECODE(b,1,'True','False') into msg from dual;
    dbms_output.put_line(msg);
END;

Error:

LINE/COL ERROR
-------- -----------------------------------------------------------------
13/16    PLS-00382: expression is of wrong type

Solution

  • If I am not wrong you can't use boolean like that. But if you can transform it into an arithmetic operation resulting into an integer value you got what you want. So try to subtract 100 from p and check whether it's a positive number or not. If it's a positive number p is greater than 100.

    CREATE OR REPLACE PROCEDURE two
    AS
        i number;
        j number;
        p number;
    
        msg varchar2(100);
    BEGIN
        i:=&x;
        j:=&y;
        p:=i*j;
    
        select DECODE(sign(p-100),1,'True','False') into msg from dual;
        dbms_output.put_line(msg);
    END;