Oracle procedures and functions pdf
Enter information for the arguments, then click Next. Use the arguments in Example For example:. Enter the source code for the procedure body, then click the Next button. Click the SQL tab to view the source code for the procedure body. If you need to make corrections, click the Previous button. When you have finished, click the Finish button. You can click the Edit button to make updates to the subprogram, such as adding additional variable declarations outside the BEGIN..
END block as in Example Click the Compile button to compile the procedure. If errors occur, correct the source code and try compiling again.
Compiling the procedure also saves any changes to the procedure. In the previous steps, you created a procedure. To find out which stored procedures or functions exist in your database, use the Object Browser. In the object list, select Procedures or Functions , then click the name of the procedure or function you want to display. These stored schema level subprograms can be accessed from SQL. Example is an example of a simple stored procedure that displays current date.
When you create a procedure or function, you can specify parameters that are passed to the procedure or function when it is called or invoked. When executed, this procedure processes an employee ID and a bonus rate. It uses the Id to select the salary and commission percentage of the employee from the employees table. If the salary is null, an exception is raised. If the employee does not receive a commission, the employee's salary is updated by the bonus rate; otherwise no update is made.
For different methods to execute call stored subprograms, see Example By default, stored procedures and functions execute with the privileges of their owner, not their current user. Such definer's rights subprograms are bound to the schema in which they reside, allowing you to refer to objects in the same schema without qualifying their names. For example, if schemas HR and OE both have a table called departments , a procedure owned by HR can refer to departments rather than the qualified HR.
You can create one instance of the procedure, and many users can call it to access their own data because invoker's rights subprograms are not bound to a particular schema.
This example is based on Example Example is an example of a function that returns a character string that contains the upper case last and first names of an employee. The example also show how to run call the function. Example is an example of a stored function that returns the calculated salary ranking for a specific employee based on the current minimum and maximum salaries of employees in the same job category. END block or from another subprogram or a package.
When calling a stored procedure or function, you can write the actual parameters using the following type of notation:. Positional notation: You specify the same parameters in the same order as they are declared in the procedure. This notation is compact, but you must specify the parameters especially literals in the correct order. Named notation: You specify the name of each parameter and its value. The order of the parameters is not significant. Mixed notation: You specify the first parameters with positional notation, then switch to named notation for the last parameters.
Example shows how you can call the stored procedure in Example Click the name of the saved SQL that contains the procedure or function code that you want to edit. Modify the source code for the procedure or function. Click the Run button if you want to execute the procedure or function. When you are finished, you can click the Save button to save the code for future use. Select Procedures or Functions in the object list, then click the subprogram you want to display. With the subprogram displayed, click Edit button to modify the subprogram code.
Click the Compile button to ensure your changes did raise any errors when executed. Compiling the subprogram also saves the changes. In the object list, select Procedures or Functions , then click the name of the procedure or function you want to drop.
You can view existing packages and package bodies with the Object Browser page. These objects include packages, subprograms, and triggers. Subprograms and packages are discussed in this section; triggers are discussed in Chapter 6, "Using Triggers". When writing packages, keep them general so they can be reused in future applications. Become familiar with the Oracle-supplied packages, and avoid writing packages that duplicate features already provided by Oracle.
Design and define package specifications before the package bodies. Place in a specification only those parts that must be visible to calling programs. That way, other developers cannot build unsafe dependencies on your implementation details.
To reduce the need for recompiling when code is changed, place as few items as possible in a package specification. Changes to a package body do not require recompiling calling procedures.
Changes to a package specification require Oracle Database XE to recompile every stored subprogram that references the package. To create and run a package specification or body in the SQL Commands page:. Use the code in Example Click the Run button to create the package specification or body. If necessary, select highlight only the specific code for creating the package specification or body before clicking the Run button. Any comments outside the package or package body block are not legal in the SQL Commands page.
In the previous steps you created a package. For information about how to execute or call a subprogram in the package, see "Calling Procedures and Functions in Packages".
You can use the Object Browser page to create packages. This section explains how to create a package specification. In the Detail pane, select Package from the Create menu. In the Create Package page, select the Specification option and click Next. After entering the code for the package specification, click the Finish button. Click the Body tab, then the Edit button to enter the source code for the package body. Click the Compile button to run the package. If errors are raised, correct the source code and try compiling again.
Compiling the package also saves any changes made to the package. In the previous steps, you created a package. To find out which packages and package bodies exist in your database, use the Object Browser.
In the object list, select Packages then click the name of the package you want to display. With the package specification displayed, click the Body tab to view the package body if it exists. The package specification provides the declaration of the subprograms.
The package body provides the contents of the subprograms. The package body provides the contents of the subprograms in the package specification. Note that the function result for employee is different from the result for Example For methods on calling subprograms in a package, see "Calling a Subprogram in a Package". Modify the source code for the package. Click the Run button if you want to execute the package.
In the object list, select Packages and then click the package you want to display. With the package specification displayed, click the Edit button to modify the package specification. You can click the Body tab to edit the source code for the package body if it exists.
Compiling the package also saves the changes. When drop a package specification, the corresponding package body is dropped also. You can choose to drop only the package body. Select Packages in the object list, then click the package you want to display. With the package specification displayed, click the Drop button to drop the package specification and package body.
You can click the Body tab and then the Drop button to drop only the packaged body if it exists. Click the Finish button to confirm that you want to drop the package specification or package body. END block or from another subprogram. Note the use of the package name as a prefix to the subprogram name.
Packages are stored in the database, where they can be shared by many applications. Thus, packages enhance reuse and improve performance in a multiple-user, multiple-application environment. You can create a package specification that is designated only to supply common variables to other packages or subprograms. With the variables in one package, they can be easily maintained for all subprograms that use the variables, rather than maintaining the variables in all the individual subprograms.
Common variables are typically used in multiple subprograms, such as a sales tax rate. If you change the value of any of those variables, then all subprograms that use the variable will get the new value without having to change anything in those individual subprograms.
You can create a package specification that is designated only to supply common types, along with common variables, to other packages or subprograms. With the types in one package, they can be easily maintained for all subprograms that use the types, rather than maintaining the types in all the individual subprograms.
This section includes a list of the most common packages with a brief description and an overview of a few useful packages. Lets you register an application name with the database for auditing or performance tracking purposes.
Is part of a set of features that clients use to receive notifications when result sets of a query have changed. The package contains interfaces that can be used by mid-tier clients to register objects and specify delivery mechanisms. Specifies the Oracle version numbers and other information useful for simple conditional compilation selections based on Oracle versions. Provides a procedure that enables you to create an error logging table so that DML operations can continue after encountering errors rather than abort and roll back.
Lets you schedule administrative procedures that you want performed at periodic intervals; it is also the interface for the job queue. Lets callers easily retrieve complete database object definitions metadata from the dictionary. Lets you suspend large operations that run out of space or reach space limits after executing for a long time, fix the problem, and make the statement resume execution. Based on their purpose parameters are classified as. RETURN is the keyword that instructs the compiler to switch the control from the subprogram to the calling statement.
Normally, parent or main block will call the subprograms, and then the control will shift from those parent block to the called subprograms. The datatype of this value is always mentioned at the time of function declaration. This subprogram unit in the Oracle database is stored as a database object. Note: Subprogram is nothing but a procedure, and it needs to be created manually as per the requirement. Once created they will be stored as database objects. In this example, we are going to create an Oracle procedure that takes the name as input and prints the welcome message as output.
We are going to use EXEC command to call procedure. Below are some of the characteristics of functions. In this program, we are going to create a function that takes the name as input and returns the welcome message as output. We are going to use anonymous block and select statement to call the function.
Here we are going to see the commonly used functions and their usage. Output: String Functions These are the functions that are used on the character datatype. Output ; Guru99 Date Functions These are functions that are used for manipulating with dates.
Output : 2 Summary In this chapter, we have learned the following. Skip to content. Report a Bug. Previous Prev.
0コメント