文章目錄
1. 編寫函數fun,其功能是:計算並輸出下列多項式的值:
Sn=1+1/1!+1/2!+1/3!+…+1/n!。
例如,在函數時從鍵盤給n輸入15,則輸出結果爲:Sn=2.718282。
將所有執行正確的PL/SQL語句記錄下來。
create or replace function fun_demo (num int)
return number
is
cnt int:=1;
last number:=1;
cc int:=1;
bb int:=1;
begin
loop
loop
cc:=cc*bb;
bb:=bb+1;
exit when bb>=cnt;
end loop;
last:=last+1/cc;
cnt:=cnt+1;
exit when cnt=num;
end loop;
return (last);
end fun_demo;
declare
last number(10,6);
begin
last:=fun_demo(&num);
dbms_output.put_line(last);
end;
2、在SCOTT模式下創建一個函數:該函數的功能爲輸入僱員號,返回emp表中該僱員姓名。並且完成對該函數的調用。將所有執行正確的PL/SQL語句記錄下來。
create or replace function name_demo(no number)
return VARCHAR2 is
name VARCHAR2(10);
begin
select ENAME into name from emp where EMPNO=no;
return name;
end name_demo;
begin
dbms_output.put_line(name_demo(&name));
end;
3、在SCOTT模式下創建一個函數:該函數的功能以部門號爲參數,返回該部門的平均工資。並且完成對該函數的調用。 將所有執行正確的PL/SQL語句記錄下來。
create or replace function em_demo(v_dept NUMBER)
return NUMBER
is
money NUMBER;
begin
select avg(sal) into money from emp where DEPTNO=v_dept;
return money;
end em_demo;
begin
dbms_output.put_line(em_demo(&no));
end;