We Recommend

SQL Cookbook SQL Cookbook
Written in O'Reilly's popular Problem/Solution/Discussion style, the SQL Cookbook is sure to please. Anthony's credo is: "When it comes down to it, we all go to work, we all have bills to pay, and we all want to go home at a reasonable time and enjoy what's still available of our days." The SQL Cookbook moves quickly from problem to solution, saving you time each step of the way.


Posted By

indianocean on 03/26/08


Tagged

sql template stored procedure Oracle


Versions (?)


Simple Oracle stored procedure


Published in: SQL 


Just a template...


  1. CREATE OR REPLACE PROCEDURE P_SUMMARY_WSP IS
  2.  
  3. cursor csr_hierarchy IS
  4. SELECT
  5. parent,
  6. child
  7. FROM
  8. hierarchy_deep
  9. ORDER BY
  10. parent;
  11.  
  12. answered_no_total number;
  13. rec_csr_hierarchy csr_hierarchy%rowtype;
  14.  
  15. BEGIN
  16. -- ** preparations
  17. execute immediate 'truncate table l_summary_wsp';
  18.  
  19. -- iterate cursor
  20. open csr_hierarchy;
  21. loop
  22. fetch csr_hierarchy INTO rec_csr_hierarchy;
  23. exit when csr_hierarchy%notfound;
  24.  
  25. -- check if we already have an entry for the current person and add
  26. -- to that, create otherwise
  27. begin
  28. SELECT
  29. answered_no INTO answered_no_total
  30. FROM
  31. pp_summary_wsp
  32. WHERE
  33. person_id = rec_csr_hierarchy.parent;
  34.  
  35. -- check for current subordinate if answered no
  36.  
  37. UPDATE
  38. pp_summary_wsp
  39. SET
  40. answered_no = answered_no_total + 1
  41. WHERE
  42. person_id = rec_csr_hierarchy.parent;
  43.  
  44. exception
  45. when NO_DATA_FOUND then
  46. INSERT INTO
  47. pp_summary_wsp
  48. (person_id, answered_no)
  49. VALUES
  50. (rec_csr_hierarchy.parent, 1);
  51. end;
  52. end loop;
  53.  
  54. -- close cursor
  55. IF csr_hierarchy%ISOPEN then
  56. close csr_hierarchy;
  57. end IF;
  58.  
  59. END P_SUMMARY_WSP;

Report this snippet 

Comments

RSS Icon Subscribe to comments
Posted By: indianocean on March 26, 2008

You need to login to post a comment.