FUNCTION [HÀM]
Trang 1 trong tổng số 1 trang
FUNCTION [HÀM]
Viết một hàm để trả về kết quả là năm của một Date (ngày-tháng-năm) được truyền vào từ tham số.
Lồng hàm GETYEAR vào câu lệnh SELECT để hiển thị bảng gồm 2 cột hiredate, hireyear trong SCOTT.EMP .
Câu 2: Viết một hàm để lấy tổng tiền lương của một phòng ban nào đó.
- Code:
create or replace function GETYEAR (f_date in Date)
return number is
begin
return extract GETYEAR(year from f_date);
exception
when others then return 0;
end;
Lồng hàm GETYEAR vào câu lệnh SELECT để hiển thị bảng gồm 2 cột hiredate, hireyear trong SCOTT.EMP .
- Code:
select hiredate, GETYEAR(hiredate) as hireyear
from scott.emp;
Câu 2: Viết một hàm để lấy tổng tiền lương của một phòng ban nào đó.
- Code:
create or replace function get_dept_sal (dno number)
return number is
all_sal number;
begin
all_sal := 0;
for emp_sal in (select sal from scott.emp where deptno = dno and sal is not null)
loop
all_sal := all_sal + emp_sal.sal;
end loop;
return all_sal;
end get_dept_sal;
Trang 1 trong tổng số 1 trang
Permissions in this forum:
Bạn không có quyền trả lời bài viết
|
|