Mega Code Archive

 
Categories / Oracle PLSQL / Cursor
 

Combine for loop and if statement to check the value in cursor

SQL> CREATE TABLE products(   2    name            VARCHAR2(50),   3    price         NUMBER(8,2),   4    min_price          NUMBER(8,2)   5  ); Table created. SQL> SQL> SQL> create or replace procedure print_products as   2      cursor dataCursor is select name, price from products;   3    begin   4       for i in dataCursor LOOP   5           if i.price > 50 then   6              dbms_output.put_line(i.name ||' Price: '|| i.price);   7           else   8              dbms_output.put_line(i.name || ' Product under 50');   9           end if;  10       END LOOP;  11   end;  12  / Procedure created. SQL> SQL> execute print_products PL/SQL procedure successfully completed. SQL> SQL> drop table products; Table dropped. SQL>