Subprogram - Procedure and Function
A subprogram is a program unit/module that performs a particular task. These subprograms are combined to form larger programs. This is basically called the 'Modular design'. A subprogram can be invoked by another subprogram or program, which is called the calling program.
A subprogram can be created:
- At schema level
- Inside a package
- Inside a PL/SQL block
A schema level subprogram is a standalone subprogram. It is created with the CREATE PROCEDURE or CREATE FUNCTION statement. It is stored in the database and can be deleted with the DROP PROCEDURE or DROP FUNCTION statement.
A subprogram created inside a package is a packaged subprogram. It is stored in the database and can be deleted only when the package is deleted with the DROP PACKAGE statement.
To create a function in your own schema, you must have the
CREATE
PROCEDURE
system privilege. To create a function in another user's schema, you must have the CREATE
ANY
PROCEDURE
system privilege. To replace a function in another user's schema, you must have the ALTER
ANY
PROCEDURE
system privilege.
To invoke a call specification, you may need additional privileges, for example,
EXECUTE
privileges on a C library for a C call specification.
To embed a
CREATE
FUNCTION
statement inside an Oracle precompiler program, you must terminate the statement with the keyword END-EXEC
followed by the embedded SQL statement terminator for the specific language.PL/SQL Package
PL/SQL packages are schema objects that groups logically related PL/SQL types, variables and subprograms.
A package will have two mandatory parts:
- Package specification
- Package body or definition
The specification is the interface to the package. It just DECLARES the types, variables, constants, exceptions, cursors, and subprograms that can be referenced from outside the package. In other words, it contains all information about the content of the package, but excludes the code for the subprograms.
All objects placed in the specification are called public objects. Any subprogram not in the package specification but coded in the package body is called a private object.
Package Body
The package body has the codes for various methods declared in the package specification and other private declarations, which are hidden from code outside the package.
The CREATE PACKAGE BODY Statement is used for creating the package body. The following code snippet shows the package body declaration for the cust_sal package created above. I assumed that we already have CUSTOMERS table created in our database as mentioned in PL/SQL - Variableschapter.
Advantages of Packages:
Packages have a long history in software engineering, offering important features for reliable, maintainable, reusable code, often in team development efforts for large systems.
Modularity
Packages let you encapsulate logically related types, items, and subprograms in a named PL/SQL module. Each package is easy to understand, and the interfaces between packages are simple, clear, and well defined. This aids application development.
Easier Application Design
When designing an application, all you need initially is the interface information in the package specs. You can code and compile a spec without its body. Then, stored subprograms that reference the package can be compiled as well. You need not define the package bodies fully until you are ready to complete the application.
Information Hiding
With packages, you can specify which types, items, and subprograms are public (visible and accessible) or private (hidden and inaccessible). For example, if a package contains four subprograms, three might be public and one private. The package hides the implementation of the private subprogram so that only the package (not your application) is affected if the implementation changes. This simplifies maintenance and enhancement. Also, by hiding implementation details from users, you protect the integrity of the package.
Added Functionality
Packaged public variables and cursors persist for the duration of a session. They can be shared by all subprograms that execute in the environment. They let you maintain data across transactions without storing it in the database.
Better Performance
When you call a packaged subprogram for the first time, the whole package is loaded into memory. Later calls to related subprograms in the package require no disk I/O.
Packages stop cascading dependencies and avoid unnecessary recompiling. For example, if you change the body of a packaged function, Oracle does not recompile other subprograms that call the function; these subprograms only depend on the parameters and return value that are declared in the spec, so they are only recompiled if the spec changes
Advantages of Subprogram:
- Subprograms let you extend the PL/SQL language. Procedures act like new statements. Functions act like new expressions and operators.
- Subprograms let you break a program down into manageable, well-defined modules. You can use top-down design and the stepwise refinement approach to problem solving.
- Subprograms promote reusability. Once tested, a subprogram can be reused in any number of applications. You can call PL/SQL subprograms from many different environments, so that you do not have to reinvent the wheel each time you use a new language or API to access the database.
- Subprograms promote maintainability. You can change the internals of a subprogram without changing other subprograms that call it. Subprograms play a big part in other maintainability features, such as packages and object types.
- Dummy subprograms (stubs) let you defer the definition of procedures and functions until after testing the main program. You can design applications from the top down, thinking abstractly, without worrying about implementation details.
Usage of Functions:
- User-defined functions cannot be used in situations that require an unchanging definition. Thus, you cannot use user-defined functions:
- In a
CHECK
constraint clause of aCREATE
TABLE
orALTER
TABLE
statement - In a
DEFAULT
clause of aCREATE
TABLE
orALTER
TABLE
statement
- In addition, when a function is called from within a query or DML statement, the function cannot:
- Have
OUT
orIN
OUT
parameters - Commit or roll back the current transaction, create a savepoint or roll back to a savepoint, or alter the session or the system. DDL statements implicitly commit the current transaction, so a user-defined function cannot execute any DDL statements.
- Write to the database, if the function is being called from a
SELECT
statement. However, a function called from a subquery in a DML statement can write to the database. - Write to the same table that is being modified by the statement from which the function is called, if the function is called from a DML statement.
Here are some particularly noteworthy advantages of placing SQL within Oracle stored procedures and packages:
- High productivity: PL/SQL is a language common to all Oracle environments. Developer productivity is increased when applications are designed to use PL/SQL procedures and packages because it avoids the need to rewrite code. Also, the migration complexity to different programming environments and front-end tools will be greatly reduced because Oracle process logic code is maintained inside the database with the data, where it belongs. The application code becomes a simple “shell” consisting of calls to stored procedures and functions.
- Improved Security: Making use of the “grant execute” construct, it is possible to restrict access to Oracle, enabling the user to run only the commands that are inside the procedures. For example, it allows an end user to access one procedure that has a command delete in one particular table instead of granting the delete privilege directly to the end user. The security of the database is further improved since you can define which variables, procedures and cursors will be public and which will be private, thereby completely limiting access to those objects inside the PL/SQL package. With the “grant” security model, back doors like SQL*Plus can lead to problems; with “grant execute” you force the end-user to play by your rules.
- Application portability: Every application written in PL/SQL can be transferred to any other environment that has the Oracle Database installed regardless of the platform. Systems that consist without any embedded PL/SQL or SQL become “database agnostic” and can be moved to other platforms without changing a single line of code.
- Code Encapsulation: Placing all related stored procedures and functions into packages allows for the encapsulation of storage procedures, variables and datatypes in one single program unit in the database, making packages perfect for code organization in your applications.
- Global variables and cursors: Packages can have global variables and cursors that are available to all the procedures and functions inside the package.
19 comments:
Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog.
Click here:
angularjs training in chennai
Click here:
angularjs2 training in chennai
Click here:
angularjs4 Training in Chennai
Click here:
angularjs5 Training in Chennai
Click here:
angularjs training in tambaram
I’ve desired to post about something similar to this on one of my blogs and this has given me an idea. Cool Mat.
Click here:
Microsoft azure training in velarchery
Click here:
Microsoft azure training in sollinganallur
Click here:
Microsoft azure training in btm
Click here:
Microsoft azure training in rajajinagar
Wonderful bloggers like yourself who would positively reply encouraged me to be more open and engaging in commenting.So know it's helpful.
Blueprism training in Chennai
Blueprism training in Bangalore
Blueprism training in Pune
Blueprism online training
Blueprism training in tambaram
Thank you for benefiting from time to focus on this kind of, I feel firm about it and also really like comprehending far more with this particular subject matter. In case doable, when you get know-how, is it possible to thoughts modernizing your site together with far more details? It’s extremely useful to me
Data Science course in kalyan nagar | Data Science course in OMR
Data Science course in chennai | Data science course in velachery
Data science course in jaya nagar | Data science training in tambaram
I read this post two times, I like it so much, please try to keep posting & Let me introduce other material that may be good for our community.
java training in chennai | java training in USA
Well you use a hard way for publishing, you could find much easier one!
angularjs Training in marathahalli
angularjs interview questions and answers
angularjs Training in bangalore
angularjs Training in bangalore
angularjs Training in chennai
Thanks for posting this info. I just want to let you know that I just check out your site and I find it very interesting and informative. I can't wait to read lots of your posts
informatica mdm online training
apache spark online training
angularjs online training
devops online training
aws online training
I love the blog. Great post. It is very true, people must learn how to learn before they can learn. lol i know it sounds funny but its very true. . .
Microsoft Azure online training
Selenium online training
Java online training
Python online training
uipath online training
Hi,
Good job & thank you very much for the new information, i learned something new. Very well written. It was sooo good to read and usefull to improve knowledge. Who want to learn this information most helpful. One who wanted to learn this technology IT employees will always suggest you take python training in bangalore. Because Python course in Bangalore is one of the best that one can do while choosing the course.
excellent...!
internship in chennai for ece students
internships in chennai for cse students 2019
Inplant training in chennai
internship for eee students
free internship in chennai
eee internship in chennai
internship for ece students in chennai
inplant training in bangalore for cse
inplant training in bangalore
ccna training in chennai
this is amazing platform to learn many new things thank you for creating this blog
BEST ANGULAR JS TRAINING IN CHENNAI WITH PLACEMENT
https://www.acte.in/angular-js-training-in-chennai
https://www.acte.in/angular-js-training-in-annanagar
https://www.acte.in/angular-js-training-in-omr
https://www.acte.in/angular-js-training-in-porur
https://www.acte.in/angular-js-training-in-tambaram
https://www.acte.in/angular-js-training-in-velachery
Great article. Keep sharing more.
AngularJS training in chennai | AngularJS training in anna nagar | AngularJS training in omr | AngularJS training in porur | AngularJS training in tambaram | AngularJS training in velachery
thanks a lot for shring thi onderful blog.i really happy to read this content.
Ai & Artificial Intelligence Course in Chennai
PHP Training in Chennai
Ethical Hacking Course in Chennai Blue Prism Training in Chennai
UiPath Training in Chennai
Trending technology.It is very informative to building my career.
Java training in Chennai
Java training in Bangalore
Java training in Hyderabad
Java Training in Coimbatore
Java Online Training
I really enjoy simply reading all of your weblogs. Simply wanted to inform you that you have people like me who appreciate your work. Definitely a great post.
Angular js Training in Chennai
Angular js Training in Velachery
Angular js Training in Tambaram
Angular js Training in Porur
Angular js Training in Omr
Angular js Training in Annanagar
I really enjoy simply reading all of your weblogs. Simply wanted to inform you that you have people like me who appreciate your work. Definitely a great post.
IELTS Coaching in chennai
German Classes in Chennai
GRE Coaching Classes in Chennai
TOEFL Coaching in Chennai
Spoken english classes in chennai | Communication training
Nice & Informative Blog !
you may encounter various issues in QuickBooks that can create an unwanted interruption in your work. To alter such problems, call us at QuickBooks Customer Support Number 1-855-974-6537 and get immediate technical services for QuickBooks in less time.
Hey! If you’ve got QuickBooks Error 1935 on your PC, then do call us and get immediate ways to solve QuickBooks problems. Our team is highly qualified and offers you the best possible solutions to troubleshoot all the queries of QuickBooks in less time.
nice......................!
micro strategy certification training
msbi course training
Post a Comment