Suppose I have an Oracle 11.2 database containing the following table:
TABLE: SURVEY
PARAMETER MALE FEMALE
--------------------------
SAMPLE_SIZE 102 95
AVG_WEIGHT 170 120
AVG_HEIGHT 5.9 5.4
Here's an example minimal PL/SQL stored procedure that selects the average weight of males and places it (e.g. 170) into variable v_stat.
PROCEDURE get_stat (gender IN VARCHAR2)
AS
v_stat number;
BEGIN
SELECT male INTO v_stat FROM survey WHERE parameter = 'avg_weight';
END get_stat;
Notice the IN parameter gender doesn't do anything here. What I want is to pass in variable gender, which may equal either 'male' or 'female', and use gender somehow in the SELECT statement (instead of 'male'). The goal is to pass in gender as a variable that may be used to return, for example, the average weight of, either male or female, as defined by gender.
I know I can probably use an IF/THEN/ELSE statement with two separate SELECT statements, but I wondered if there was an elegant way to use just one SELECT statement by changing 'male' in the above SELECT statement to something else?
Note that this is a re-do of my previous question here
How to programmatically set table name in PL/SQL?
that was (rightly) criticized for not being a realistic question.