Skip to content

Commit ccd21a6

Browse files
committed
Convert show_departments() from procedure to function returning TABLE
PL/pgSQL procedures cannot return result sets directly with bare SELECT. Convert to a function returning TABLE, which is the idiomatic PostgreSQL approach. Update CI and README to use SELECT * FROM show_departments().
1 parent c22965d commit ccd21a6

3 files changed

Lines changed: 12 additions & 10 deletions

File tree

.github/workflows/ci-postgresql.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ jobs:
105105
$SBDIR/use -d employees -t -c "SELECT emp_name(10001);"
106106
$SBDIR/use -d employees -t -c "SELECT emp_dept_name(10001);"
107107
$SBDIR/use -d employees -t -c "SELECT current_manager('d001');"
108-
$SBDIR/use -d employees -c "CALL show_departments();"
108+
$SBDIR/use -d employees -c "SELECT * FROM show_departments();"
109109
$SBDIR/use -d employees -t -c "SELECT COUNT(*) FROM v_full_employees;"
110110
$SBDIR/use -d employees -t -c "SELECT COUNT(*) FROM v_full_departments;"
111111

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,7 @@ The database is also available for PostgreSQL. The schema and data are identical
130130

131131
psql -d employees < postgresql/objects.sql
132132

133-
Available functions: `emp_name()`, `emp_dept_name()`, `emp_dept_id()`, `current_manager()`.
134-
Available procedures: `CALL show_departments()`, `CALL employees_help()`.
133+
Available functions: `emp_name()`, `emp_dept_name()`, `emp_dept_id()`, `current_manager()`, `show_departments()` (use `SELECT * FROM show_departments();`), `employees_help()`.
135134

136135

137136
## DISCLAIMER

postgresql/objects.sql

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ DROP FUNCTION IF EXISTS emp_dept_name(INT);
88
DROP FUNCTION IF EXISTS emp_name(INT);
99
DROP FUNCTION IF EXISTS current_manager(CHAR(4));
1010
DROP FUNCTION IF EXISTS employees_usage();
11-
DROP PROCEDURE IF EXISTS show_departments();
11+
DROP FUNCTION IF EXISTS show_departments();
1212
DROP PROCEDURE IF EXISTS employees_help();
1313

1414
--
@@ -126,7 +126,8 @@ FROM
126126
-- shows the departments with the number of employees
127127
-- per department
128128
--
129-
CREATE OR REPLACE PROCEDURE show_departments()
129+
CREATE OR REPLACE FUNCTION show_departments()
130+
RETURNS TABLE(dept_no CHAR(4), dept_name VARCHAR(40), manager VARCHAR(32), count BIGINT)
130131
LANGUAGE plpgsql
131132
AS $$
132133
BEGIN
@@ -164,11 +165,12 @@ BEGIN
164165
AND dmd.dept_to_date=de.to_date
165166
AND dmd.emp_no=de.emp_no;
166167

168+
RETURN QUERY
167169
SELECT
168-
dept_no, dept_name, manager, COUNT(*)
169-
FROM v_full_departments
170-
INNER JOIN department_people USING (dept_no)
171-
GROUP BY dept_no, dept_name, manager;
170+
v.dept_no, v.dept_name, v.manager::VARCHAR(32), COUNT(*)::BIGINT
171+
FROM v_full_departments v
172+
INNER JOIN department_people dp ON v.dept_no = dp.dept_no
173+
GROUP BY v.dept_no, v.dept_name, v.manager;
172174

173175
DROP TABLE department_max_date;
174176
DROP TABLE department_people;
@@ -185,10 +187,11 @@ BEGIN
185187
== USAGE ==
186188
====================
187189
188-
PROCEDURE show_departments()
190+
FUNCTION show_departments()
189191
190192
shows the departments with the manager and
191193
number of employees per department
194+
(returns TABLE - use: SELECT * FROM show_departments();)
192195
193196
FUNCTION current_manager (dept_id)
194197

0 commit comments

Comments
 (0)