6. CREATE PROCEDURE Command
In Oracle we can create our own procedure. A procedure is a collection of SQL statements that can be called by any valid object name. To create a user-defined procedure we use the command "CREATE PROCEDURE".
While creating a procedure or function we always state parameters that we can pass in three ways:
| Parameters | Referenced by Procedure | Value overwritten by Procedure |
| IN | Yes | NO |
| OUT | NO | YES |
| INOUT | YES | YES |
Syntax
CREATE [OR REPLACE] PROCEDURE procedure_name
[ (parameter [,parameter]) ]
IS
Declaration section
BEGIN
Execution section
EXCEPTION
Exception_section
END;
Example: Assuming the following table:
| Emp_ID | Emp_Name | Designation | Salary |
| TCS008 | Vivan | Project Manager | 1Lac |
| TCS030 | Seema | Tech Lead | 80K |
Example
CREATE OR REPLACE PROCEDURE
Employee_details
IS
Cursor Emp_Cur IS
SELECT Name, Designation, Salary FROM Employee
emp_rec emp_cur%rowtype;
BEGIN
FOR Emp_rec in Sales_Cur
LOOP
Dbms_output.put_line(Emp_cur.Name || '||emp_cur.Designation ||' '||Emp_cur.Salary);
END LOOP
END;
7. CREATE SCHEMA Command
A group of database objects is called a schema and is owned by a database user and must have the name as the database user. The "Create schema" command creates the multiple tables and a view.
Syntax
CREATE SCHEMA AUTHORIZATION Schema options;
Options
The options are:
- CREATE TABLE
- CREATE VIEW
- GRANT
Example
CREATE SCHEMA AUTHORIZATION Student
CREATE TABLE BOOK
(
Book_id NUMBER PRIMARY KEY,
Book_Price NUMBER,
Author Varchar2(10),
Seller Varchar2(10),
Publications Varchar2(30)
)
CREATE VIEW New_Book AS
SELECT Book_id, Author FROM Book WHERE Book_Price = 500
GRANT SELECT ON New_Book TO Scott;
8. CREATE FUNCTION Command
The CREATE FUNCTION statement creates a new function with a unique name in a specific schema if it has the schema name, otherwise the function is created in the current schema. The owner of the new function is the user that created the same function.
Syntax
CREATE [OR REPLACE] FUNCTION function_name [parameter]
RETURN return_datatypes;
IS
Declaration_Section
BEGIN
Execution_Section
RETURN Return_variable;
EXCEPTION
Exception Section
RETURN Return_variable;
END;
OR
CREATE [OR REPLACE] FUNCTION function_name
[parameter_name [IN | OUT | INOUT] type [,….])]
RETURN return_datatype {IS | AS}
BEGIN
<function_body>
END [function_name];
Example
Assume the following table (STUDENT):
| Stu_ID | Name | Age | Stream |
| 1 | Rajat | 18 | Science |
| 2 | Jaya | 20 | Commerce |
| 3 | joseph | 19 | Arts |
| 4 | Maria | 18 | Science |
CREATE OR REPLACE FUNCTION total_students
RETURN number IS
total_number(2) :=0;
BEGIN
SELECT count(*) into total
FROM Students;
RETURN total;
END;
9. CREATE SEQUENCE Command
An Oracle sequence is an object used to generate incrementing and decrementing numbers. It is a data object from which multiple users may generate unique integers. Once a sequence is created its values can be accessed in a SQL statement.
Syntax
CREATE SEQUENCE <Sequence_name>
INCREMENT BY <integer>
START WITH <integer>
MAXVALUE <integer> / NOMAXVALUE
MINVALUE <integer> / NOMINVALUE
CYCLE/NOCYCLE

Comments
Join the conversation! Your thoughts help the community grow.