假設需要一個返回擁有特定工作的所有職員的姓名和職員號的函數,函數的參數是這個工作的頭銜。下面是執行這個任務的表函數示例: CREATE FUNCTION jobemployees (job VARCHAR(8))
RETURNS TABLE (
empno CHAR(6),
firstname VARCHAR(12),
lastname VARCHAR(15)
)
LANGUAGE SQL
READS SQL DATA
NO EXTERNAL ACTION
DETERMINISTIC
RETURN
SELECT empno, firstnme, lastname
FROM employee
WHERE employee.job = jobemployees.job;
以下查詢在 FROM 子句中引用這個新的表函數,並傳遞工作頭銜 ‘CLERK’ 作為函數的參數。語法要求用關鍵字 AS 引入一個相關名稱: db2 SELECT * FROM TABLE(jobemployees('CLERK')) AS clerk
EMPNO FIRSTNAME LASTNAME
------ ------------ ---------------
000120 SEAN O'CONNELL
000230 JAMES JEFFERSON
000240 SALVATORE MARINO
000250 DANIEL SMITH
000260 SYBIL JOHNSON
000270 MARIA PEREZ
6 record(s) selected.