Search code examples
plsqloracle9iplsqldeveloper

How do you extract number of rows in Count(*), and send to a variable. (Pl/SQL)


I'm trying extract the number of rows using the count(*) with the respect of it's condition and sending it to a variable for later use.

How do I create variables for the number of rows within a table. Thank you.

VARIABLE 
VAR CHAR(10)

SELECT COUNT(*) INTO :VAR 

FROM [Database]
WHERE [condition 1]
AND [condition 2]

Solution

  • You need to use PL/SQL to do the select:

    SQL> variable var number
    SQL> begin
      2     select count(*)
      3     into :var
      4     from all_tables
      5     where owner = user;
      6  end;
      7  /
    
    PL/SQL procedure successfully completed.
    
    SQL> print var
    
           VAR
    ----------
           163