0

I have written a stored procedure that I was expecting to add two count values into a collection, with the below code:

PROCEDURE generate(code_in in VARCHAR2
                 , value_1 out NUMBER
                 , value_2 out NUMBER) is
BEGIN
    SELECT 
        COUNT(CASE WHEN type = 'A' THEN 1 ELSE NULL END) INTO value_1
        , COUNT(CASE WHEN type IN ('B','D') THEN 1 ELSE NULL END) INTO value_2
    FROM table
    WHERE code = code_in;
END generate;

But when running the code I get the following error:

ORA-00934: group function is not allowed here

If I remove the second COUNT the stored procedure compiles just fine, but when I add this second line I get the error.

Can anyone help explain why this is happening? Please note that my experience with Oracle is minimal.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
Radu Gheorghiu
  • 20,049
  • 16
  • 72
  • 107

1 Answers1

5

into is a single clause that may receive multiple variables, not a clause you append to each select item:

PROCEDURE generate(code_in in VARCHAR2
                 , value_1 out NUMBER
                 , value_2 out NUMBER) is
BEGIN
    SELECT 
        COUNT(CASE WHEN type = 'A' THEN 1 ELSE NULL END),
        COUNT(CASE WHEN type IN ('B','D') THEN 1 ELSE NULL END)
    INTO value_1, value_2 -- into clause with both variables
    FROM table
    WHERE code = code_in;
END generate;
Mureinik
  • 297,002
  • 52
  • 306
  • 350