Stored Procedures and functions of PL/SQL programs

Source: Internet
Author: User

  1. Stored Procedures and functions
  2. A subroutine stored in a database for calls by all user programs is called a stored procedure and a stored function.
  3. Create a stored procedure:
  4. UseCREATE PROCEDURECommand to create a stored procedure and a stored function.
  5. Syntax:
  6. Create[Or Replace]PROCEDUREProcess name (parameter list)
  7. AS
  8. PLSQL subprogram body;
  9. Note: stored procedures are generally used in highly secure systems.
  10. Example 1: increase the salary of all employees by 10%
  11. Create a stored procedure
  12. Create Or Replace ProcedureUpdateSal
  13. As
  14. -- Plsql Block
  15. -- The Stored Procedure cannot have declare
  16. Create Or Replace ProcedureUpdateSal
  17. As
  18. CursorC1Is SelectSal, empnoFromEmp;
  19. Emp_sal emp. sal % type;
  20. Emp_no emp. empno % type;
  21. Begin
  22. OpenC1;
  23. Loop
  24. FetchC1IntoEmp_sal, emp_no;
  25. ExitWhenC1 % notfound;
  26. UpdateEmpSetSal = emp_sal * 1.1WhereEmpno = emp_no;
  27. EndLoop;
  28. CloseC1;
  29. End;
  30. Call Stored Procedure
  31. Method 1:
  32. SQL>Begin
  33. 2 updateSal;
  34. 3End;
  35. 4/
  36. Method 2:
  37. SQL>ExecUpdateSal;
  38. After the call is complete, submit it manually.
  39. CommitStatement: ends the current transaction, so that all the modifications executed by the current transaction are permanent.
  40. ]
  41. Storage Functions
  42. Function (Function) Is a named bucket that can contain parameters and return a calculated value. The structure of a function and a process is similar, but there must beRETURNClause to return the function value. Function Description specifies the function name, result value type, and parameter type.
  43. Syntax for creating a storage function:
  44. CREATE[OR REPLACE]FUNCTIONFunction Name (parameter list)
  45. RETURNFunction value type
  46. AS
  47. PLSQL subprogram body;
  48. Example 2: Obtain the annual income of a specified employee
  49. Create Or Replace FunctionSumSal (emp_no number)-- Function (the parameter value must have a type)
  50. -- Return Value Type
  51. ReturnNumber-- The return value must exist.
  52. As
  53. -- Declare Variables
  54. Emp_sal emp. sal % type;
  55. Emp_comm emp. comm % type;
  56. Total emp. sal % type;
  57. Begin
  58. SelectSal, commIntoEmp_sal, emp_commFromEmpWhereEmpno = emp_no;
  59. Total: = emp_sal * 12 + nvl (emp_comm, 0 );
  60. ReturnTotal;-- The return value must be of the same type.
  61. End;
  62. Call Method
  63. Method 1:
  64. SQL>Begin
  65. 2 dbms_output.put_line (sumSal (7369 ));
  66. 3End;
  67. 4/
  68. Method 2:
  69. Declare
  70. V_sal number;
  71. Begin
  72. V_sal: = sumSal (1, 7369 );
  73. Dbms_output.put_line (v_sal );
  74. End;
  75. Result
  76. 12777.6

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.