QQ登录

只需一步,快速开始

Create a function to calculate a shopper’s total spending

[复制链接]
admin 发表于 2022-3-12 07:30:06 [显示全部楼层] 回帖奖励 倒序浏览 阅读模式 0 746
Create a function to calculate a shopper’s total spending, excluding shipping and tax amount, with Brewbean’s site in a particular year. Exception handling is needed. Use an anonymous block to call the function and output the result.


  1. Expert Solution
  2. arrow_forward
  3. Step 1
  4. Below is the function totalSpending to calculate a shopper’s total spending, excluding shipping and tax amount, with Brewbean’s site in a particular year.

  5. arrow_forward
  6. Step 2
  7. CREATE OR REPLACE FUNCTION totalSpending

  8. RETURN number IS

  9. finalspending number(2) := 0;

  10. DECLARE

  11. id integer := 0;

  12. shipping_amount integer := 0;

  13. tax_amount integer := 0;

  14. total_spending integer  :=0;

  15. year_var integer := 0;

  16. BEGIN

  17. ACCEPT x number prompt "Enter shopper ID: ";

  18. id := &x;

  19. ACCEPT y number prompt "Enter year: ";

  20. year_var := &y;

  21. SELECT SUM(shipping_amount) INTO shipping_amount

  22. FROM table_name

  23. WHERE shopper_id = id AND year = year_var;

  24. SELECT SUM(tax_amount) INTO tax_amount

  25. FROM table_name

  26. WHERE shopper_id = id AND year = year_var;

  27. SELECT SUM(spending) INTO total_spending

  28. FROM table_name

  29. WHERE shopper_id = id AND year = year_var;

  30. finalSpending := total_spending - (shipping_amount + tax_amount);

  31. return finalSpending;

  32. EXCEPTION:

  33. WHEN no_data_found then:

  34. dbms_output.put_line("No such shopper")

  35. WHEN others then:

  36. dbms_output.put_line("Error")

  37. END;

  38. BEGIN

  39. finalSpending := totalSpending();

  40. dbms_output.put_line(finalSpending);

  41. END;

  42. /

复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

官方微博
官方微博
模板大全
模板大全
意见
反馈