最近在學pl/sql。
老師很酸,同學很懶。所以我想我應該寫一下這些東西記錄一 下,如果有人需要的話,不妨參考看看?
題目是這樣的
Using collection variable to store department name and number of employees working for this department, then navigate the entire collect variable to print out all records.
簡單翻譯一下,就是用collection variable把部門名稱跟部門號碼print out 出來。想當然耳,在完成這條件之前,你需要有一個sql table 叫做department. 然後,雖然這不一定必要(我很懷疑XD),但是我想你應該需要知道一下,如何用SQL statements 去完成這個題目。
SQL statement:
select department_name, department_ID from department;
然後是PL/SQL的寫法:
set serveroutput on
declare
TYPE test_type IS TABLE OF VARCHAR2(100)
INDEX BY BINARY_INTEGER;
test_table test_type;
BEGIN
FOR crec IN (select department_ID id, department_ID||' '||department_name AS name from employees) LOOP
test_table(crec.id) := crec.name;
dbms_output.put_line(cust_table(crec.id));
END LOOP;
END;
/
以上。