tychopycho
tychopycho

Reputation: 41

How to count values in pl/sql

I'm new to pl/sql and I'm trying to count all the employees that have the initials 'H'
My code currently looks like this but I get an error on line 24, COUNT(v_initials);

declare

    cursor cursor_employee
    is 
    select * from cmp_employer_employees
    where rownum < 25
    order by employer_employee_id asc
    ;
    
    v_initials varchar(100);
    v_count number := 0;
    
begin
    
    for r_employee in cursor_employee loop
    
        v_initials := r_employee.INITIALS;
    
        if v_initials like '%H' 
        then
        
            COUNT(v_initials);
        
        end if;
    
    end loop;

end;

Upvotes: 1

Views: 412

Answers (1)

Mureinik
Mureinik

Reputation: 312136

You don't need a loop there - you could query count(*) from the table with a condition:

SELECT COUNT(*)
FROM   employees
WHERE  initials LIKE '%H'

EDIT
To answer the question in the comment, you can use an insert-select statement to collect these employees to another table:

INSERT INTO employees_with_h
SELECT * 
FROM   employees
WHERE  initials LIKE '%H'

Or, if you want just a few specific columns:

INSERT INTO employees_with_h (target_col1, target_col2, etc)
SELECT col1, col2, etc
FROM   employees
WHERE  initials LIKE '%H'

Upvotes: 2

Related Questions