創建一個PL/SQL塊,計算一年的補償金額。
阿新 • • 發佈:2017-10-16
roc 提示 lin 處理 enter dbm number 第一個 str
創建一個PL/SQL塊,計算一年的補償金額。
【要求】
2 通過SQL *Plus替代變量將每年的工資和每年的獎金比率傳遞給PL/SQL塊
2 獎金比率為整數,在塊中將其轉換為小數形式(如15轉換成0.15)。
2 如果工資為空,在計算全部補償金額時將該工資設為0。
【提示】:使用NVL函數來處理空值。
執行後的顯示如下:
Please enter the salary amount: 50000
Please enter the bonus percentage: 10
PL/SQL procedure successfully completed.
G_TOTAL
-------
55000
SQL>accept p_sal prompt ‘Please enter the salary amount:‘ Please enter the salary amount:50000 SQL> accept p_bonus prompt ‘Please enter the bonus percentage:‘ Please enter the bonus percentage:10 SQL> variable g_total number; SQL> begin 2 :g_total:=nvl(&p_sal,0)*(1+&p_bonus*0.01); 3 end; 4 / 原值 2: :g_total:=nvl(&p_sal,0)*(1+&p_bonus*0.01); 新值 2: :g_total:=nvl(50000,0)*(1+10*0.01); PL/SQL 過程已成功完成。 SQL> print g_total; G_TOTAL ---------- 55000
知識點:
1.accpet :可以來接受預先指定變量值,來改變提示語句
SQL> accept p_sal prompt ‘Please enter the salary amount:‘ Please enter the salary amount:50000
2.nvl函數
補充: NVL2函數
NVL2函數的格式如下:NVL2(expr1,expr2, expr3)
含義是:如果該函數的第一個參數為空那麽顯示第二個參數的值,如果第一個參數的值不為空,則顯示第三個參數的值。
SQL> select ename,NVL2(comm,-1,1) from emp;
NULLIF函數
SQL> declare 2 v_num1 number:=2; 3 v_num2 number:=2; 4 a number; 5 begin 6 select nullif(v_num1,v_num2)into a from dual; 7 dbms_output.put_line(‘a:‘||a); 8 end; 9 / a: PL/SQL 過程已成功完成。 SQL> declare 2 v_num1 number:=2; 3 v_num2 number:=3; 4 a number; 5 begin 6 select nullif(v_num1,v_num2)into a from dual; 7 dbms_output.put_line(a); 8 end; 9 / 2 PL/SQL 過程已成功完成。
創建一個PL/SQL塊,計算一年的補償金額。