CASE statement.........

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nkumarin001
    New Member
    • Jun 2007
    • 38

    CASE statement.........

    Hi,
    Can anyone please help me to rectify the problem.

    Hi i wrote the following code regarding CASE statement:-

    declare
    num number :=45;
    begin
    case num
    when num<20 then
    dbms_output.put _line('Num is less then 20');
    when num<40 then
    dbms_output.put _line('Num is less then 40');
    else
    dbms_output.put _line('Num is less then 60');
    end case;
    end;
    /

    but i got the following error:-

    case num
    *
    ERROR at line 4:
    ORA-06550: line 4, column 6:
    PLS-00615: type mismatch found at 'NUM' between CASE operand and WHEN operands
    ORA-06550: line 4, column 1:
    PL/SQL: Statement ignored

    ********** In this error i think datatype mismatch occured but i dont no why it occured **********

    Please help with the correct code.

    Regards,
    Naveen
  • debasisdas
    Recognized Expert Expert
    • Dec 2006
    • 8119

    #2
    Use the following code

    [CODE=oracle]
    declare
    num number :=45;
    begin
    case
    when num < 20 then
    dbms_output.put _line('Num is less then 20');
    when num < 40 then
    dbms_output.put _line('Num is less then 40');
    else
    dbms_output.put _line('Num is less then 60');
    end case;
    end;[/CODE]

    Comment

    • Kavitha Kusum
      New Member
      • Jul 2007
      • 2

      #3
      What i think is that
      the num which u used after case is of number data type and the num which u used in the when is returning you true or false, so there is a mismatch between the datatypes.

      Comment

      • debasisdas
        Recognized Expert Expert
        • Dec 2006
        • 8119

        #4
        Originally posted by Kavitha Kusum
        What i think is that
        the num which u used after case is of number data type and the num which u used in the when is returning you true or false, so there is a mismatch between the datatypes.
        There is nothing like mismatch.
        Just compare the two sample code , you will get the difference.

        Comment

        • holdingbe
          New Member
          • Jul 2007
          • 78

          #5
          Originally posted by nkumarin001
          Hi,
          Can anyone please help me to rectify the problem.

          Hi i wrote the following code regarding CASE statement:-

          declare
          num number :=45;
          begin
          case num
          when num<20 then
          dbms_output.put _line('Num is less then 20');
          when num<40 then
          dbms_output.put _line('Num is less then 40');
          else
          dbms_output.put _line('Num is less then 60');
          end case;
          end;
          /

          but i got the following error:-

          case num
          *
          ERROR at line 4:
          ORA-06550: line 4, column 6:
          PLS-00615: type mismatch found at 'NUM' between CASE operand and WHEN operands
          ORA-06550: line 4, column 1:
          PL/SQL: Statement ignored

          ********** In this error i think datatype mismatch occured but i dont no why it occured **********

          Please help with the correct code.

          Regards,
          Naveen
          Hi naveen,
          I looked your query.when you use reletional opertor please don't use num infront of case statement.

          Your Query:

          declare
          num number :=45;
          begin
          case num
          when num<20 then
          dbms_output.put _line('Num is less then 20');
          when num<40 then
          dbms_output.put _line('Num is less then 40');
          else
          dbms_output.put _line('Num is less then 60');
          end case;
          end;
          your mistake is highlighted in the form of bold

          Correct Query:
          declare
          num number :=45;
          begin
          case
          when num<20 then
          dbms_output.put _line('Num is less then 20');
          when num<40 then
          dbms_output.put _line('Num is less then 40');
          else
          dbms_output.put _line('Num is less then 60');
          end case;
          end;

          Please check and tell me..


          Regards
          Michael

          Comment

          • nkumarin001
            New Member
            • Jun 2007
            • 38

            #6
            Originally posted by holdingbe
            Hi naveen,
            I looked your query.when you use reletional opertor please don't use num infront of case statement.

            Your Query:

            declare
            num number :=45;
            begin
            case num
            when num<20 then
            dbms_output.put _line('Num is less then 20');
            when num<40 then
            dbms_output.put _line('Num is less then 40');
            else
            dbms_output.put _line('Num is less then 60');
            end case;
            end;
            your mistake is highlighted in the form of bold

            Correct Query:
            declare
            num number :=45;
            begin
            case
            when num<20 then
            dbms_output.put _line('Num is less then 20');
            when num<40 then
            dbms_output.put _line('Num is less then 40');
            else
            dbms_output.put _line('Num is less then 60');
            end case;
            end;

            Please check and tell me..


            Regards
            Michael
            Hi Michael

            Thanks a lot michael its working properly.

            Regards
            Naveen

            Comment

            • nkumarin001
              New Member
              • Jun 2007
              • 38

              #7
              Originally posted by debasisdas
              Use the following code

              [CODE=oracle]
              declare
              num number :=45;
              begin
              case
              when num < 20 then
              dbms_output.put _line('Num is less then 20');
              when num < 40 then
              dbms_output.put _line('Num is less then 40');
              else
              dbms_output.put _line('Num is less then 60');
              end case;
              end;[/CODE]

              Hi debasisdas
              Thanks a lot its working fine. But i am confused why i got that error.
              Regards,
              Naveen

              Comment

              • debasisdas
                Recognized Expert Expert
                • Dec 2006
                • 8119

                #8
                because you specified the case expression twice.

                Comment

                Working...