Professional Documents
Culture Documents
Oracle 10g Made Easy
Oracle 10g Made Easy
Oracle 10g Made Easy
Training Name:
Oracle is the world’s largest business software company which provide simple to deploy,
packaged, enterprise-class software solutions to organizations. This Training curriculum
is developed to teach you skills that directly align with real-life IT jobs. The course offers
a hands-on approach to the Oracle11g database and Developer Suite Fusion Middleware.
You will learn SQL, PLSQL, FORMS, REPORTS and DBA Fundamentals of Oracle 11g
in a single package.
This Oracle training gives you strong fundamentals to cope with todays business needs.
You'll discover professional tips and techniques for brilliant and distinctive output as an
Oracle Programmer and/or DBA.
This course will make you skilled IT professional that will lead you towards excellence.
Each student will be able to get Oracle database and programming knowledge acquired in
class and use it immediately with success in the corporate world. The rigorous pace of the
course will benefit the student immensely. The student will be able to work as a Data
Architect, Data Analyst, Business Systems Analyst or Oracle Programmer in any
organization. The course will prepare the student for the challenges of these demanding
positions with specialized emphasis on Oracle technology.
Professional technique of Programming and DBA can build a robust ERP within a very
short period of time with less number of Programmers. In addition, your superior will
notice that you are skilled with Programming, DBA and Analysys activities. This of
course leads to promotion more quickly. Certification magazine found that Oracle
Professionals earn higher salaries when compared to other DBA or Developer
professionals.
He has experience from different financial institutes which includes Life Insurance ERPs,
Human Resource Management, Inventory Management etc. Currently, he is working in a
Life Insurance company as an IT Incharge.
Apart from his technical and creative contributions to the company, Mr. Muhammad
Abdullah Al Noor is an Oracle Trainer at various IT Training Institutes. He has
successfully conducted a good number of sessions on Oracle Database and Programming
up to advance level.
Mr. Muhammad Abdullah Al Noor is an MBA (MIS) and OCP (Oracle Certified
Professional).
Table of Contents
What is Oracle?
Larry Ellison and his friends, former co-workers Bob Miner and Ed Oates, started the
consultancy Software Development Laboratories (SDL) in 1977. SDL developed the
original version of the Oracle software. The name Oracle comes from the code-name of a
CIA-funded project Ellison had worked on while previously employed by Ampex.
Web Links :
http://education.oracle.com/pls/web_prod-plq-
dad/db_pages.getpage?page_id=458&get_params=p_track_id:PLSQL
http://education.oracle.com/pls/web_prod-plq-
dad/db_pages.getpage?page_id=458&get_params=p_track_id:Datab10g
Oracle offers a complete, integrated set of application development tools that support any
development approach, technology platform, or operating system.
Certification:
Oracle PL/SQL Developer Certified Associate.
Certification:
Oracle Forms Developer Certified Professional (OCP)
Certification:
Oracle Database 10g Administrator Certified Associate.(OCP)
Certification: (You must complete one of the course (1Z0-042 or 1Z0-043) from an oracle
authorized institution):
Certification:
1. tnsnames.ora :
TNSNAMES.ORA is a SQL*Net configuration file that defines databases addresses for
establishing connections to them. This file normally resides in the ORACLE
HOME\NETWORK\ADMIN directory.
E.g. E:\oracle\product\10.2.0\db_1\network\ADMIN\tnsnames.ora
The TNSNAMES.ORA files are located on both client and server systems. If you make
configuration changes on the server ($ORACLE_HOME/network/admin/tnsnames.ora)
ensure you can connect to the database through the listener if you are logged on to the
server. If you make configuration changes on the client
(g:\oracle\product\10.2.0\db_1\network\admin\tnsnames.ora) ensure you can connect
from your client workstation to the database through the listener running on the server.
Sample :
ORA11 =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = ORA11)
)
)
Configuration Here:
ORA11 = Database Name.
HOST = 127.0.0.1 or Computer Name/Hostname
SERVICE_NAME = Service Name.
2. Net Manager :
Oracle Net Manager is a graphical user interface tool that combines configuration
abilities with Oracle Names component control to provide an integrated environment for
configuring and managing Oracle Net. It can be used on either the client or server. Oracle
Net Manager is also integrated with Oracle Enterprise Manager.
You can use Oracle Net Manager to configure the following network components:
If an Oracle Names server is configured, you can start, stop, tune, or gather statistics for it
with Oracle Net Manager.
Start > Programs > Oracle - HOME_NAME > Configuration and Migration Tools > Net Manager.
It can also be run after installation in standalone mode to configure naming method
usage, the listener, net service names in the tnsnames.ora file, and directory server
usage in a similar way that is provided during installation.
Start > Programs > Oracle - HOME_NAME > Configuration and Migration Tools > Net Configuration Assistant.
Tablespace :
http://hostname:5500/em
To describe a table
To re-execute a command
SQL> /
SQL> l
or
SQL> list
SQL> c/<column-name>/<new-column-name>
SQL> @all_emp
Assuming you have made a lot of queries during a session and you want to save
everthing at once
Any other command you type will be stored inside the <filename>.txt. But before
this can happen, you need to do this:
So when you do the above, you can then exit sqlplus, and cat output.txt. The result will
show you all the commands or work you have done while in sqlplus.
SQL> ed
SQL> host
To echo something
SQL> select empno,ename,job,sal,comm,from emp where sal > &&x and comm
> &&x
SQL> passw
SQL Statements
SELECT Query
INSERT
UPDATE
Data Manipulation Language (DML)
DELETE
MERGE
CREATE
ALTER
DROP Data Definition Language (DDL)
RENAME
TRUNCATE
GRANT
Data Control Language (DCL)
REVOKE
COMMIT
ROLLBACK Transaction Control
SAVEPOINT
SL Use Statements
1 SELECT *
Selecting All Columns
FROM departments;
3. Single-Row Functions
SL Use Statements
LOWER
Case-manipulation
1 UPPER
functions
INITCAP
CONCAT
SUBSTR
LENGTH
Character-manipulation
2 INSTR
functions
LPAD | RPAD
TRIM
REPLACE
LOWER('SQL Course') sql course
Case Manipulation
3 UPPER('SQL Course') SQL COURSE
Functions
INITCAP('SQL Course') Sql Course
SELECT employee_id, last_name, department_id
FROM employees
WHERE last_name = 'higgins';
Using Case
4 no rows selected
Manipulation Functions
SELECT employee_id, last_name, department_id
FROM employees
WHERE LOWER(last_name) = 'higgins';
CONCAT('Hello', 'World') HelloWorld
SUBSTR('HelloWorld',1,5) Hello
LENGTH('HelloWorld') 10
Character-Manipulation INSTR('HelloWorld', 'W') 6
5
Functions LPAD(salary,10,'*') *****24000
RPAD(salary, 10, '*') 24000*****
TRIM('H' FROM elloWorld
'HelloWorld')
SELECT employee_id, CONCAT(first_name, last_name) NAME,
job_id, LENGTH (last_name),
Using the Character-
6 INSTR(last_name, 'a') "Contains 'a'?"
Manipulation Functions
FROM employees
WHERE SUBSTR(job_id, 4) = 'REP';
ROUND: Rounds value to specified decimal
ROUND(45.926, 2) 45.93
TRUNC: Truncates value to specified decimal
7 Number Functions
TRUNC(45.926, 2) 45.92
MOD: Returns remainder of division
MOD(1600, 300) 100
SELECT ROUND(45.923,2), ROUND(45.923,0),
Using the ROUND
8 ROUND(45.923,-1)
Function
FROM DUAL;
SELECT TRUNC(45.923,2), TRUNC(45.923),
Using the TRUNC
9 TRUNC(45.923,-2)
Function
FROM DUAL;
SELECT last_name, salary, MOD(salary, 5000)
Using the MOD
10 FROM employees
Function
WHERE job_id = 'SA_REP';
Number of months
11 Date Functions MONTHS_BETWEEN
between two dates
ADD_MONTHS Add calendar months to date
NEXT_DAY Next day of the date specified
LAST_DAY Last day of the month
SELECT COUNT(commission_pct)
FROM employees
WHERE department_id = 80;
6. Subqueries
SL Use Statements
SELECT last_name
FROM employees
Using a Subquery WHERE salary >
1
(SELECT salary
FROM employees
WHERE last_name = 'Abel');
SELECT last_name, job_id, salary
FROM employees
WHERE job_id =
Executing Single-Row
(SELECT job_id
2 Subqueries
FROM employees
WHERE employee_id = 141)
AND salary >
(SELECT salary
FROM employees
WHERE employee_id = 143);
SELECT last_name, job_id, salary
FROM employees
Using Group Functions
WHERE salary =
3 in a Subquery
(SELECT MIN(salary)
FROM employees);
7.Manipulating Data
SL Use Statements
INSERT INTO departments(department_id, department_name,
1 Inserting New Rows manager_id, location_id)
VALUES (70, 'Public Relations', 100, 1700);
INSERT INTO departments (department_id,
department_name )
Inserting Rows with VALUES (30, 'Purchasing');
2
Null Values
INSERT INTO departments
VALUES (100, 'Finance', NULL, NULL);
INSERT INTO employees (employee_id,
first_name, last_name,
email, phone_number,
hire_date, job_id, salary,
3 Inserting Special Values
commission_pct, manager_id,
department_id)
VALUES (113,
'Louis', 'Popp',
'LPOPP', '515.124.4567',
SYSDATE, 'AC_ACCOUNT', 6900,
NULL, 205, 100);
INSERT INTO employees
VALUES (114,
Inserting Specific Date 'Den', 'Raphealy',
4
Values 'DRAPHEAL', '515.127.4561',
TO_DATE('FEB 3, 1999', 'MON DD, YYYY'),
'AC_ACCOUNT', 11000, NULL, 100, 30);
INSERT INTO departments
5 Creating a Script (department_id, department_name, location_id)
VALUES (&department_id, '&department_name',&location);
INSERT INTO sales_reps(id, name, salary, commission_pct)
Copying Rows SELECT employee_id, last_name, salary, commission_pct
6
from Another Table FROM employees
WHERE job_id LIKE '%REP%';
UPDATE employees
SET department_id = 70
Updating Rows in a WHERE employee_id = 113;
7
Table
UPDATE copy_emp
SET department_id = 110;
UPDATE employees
SET job_id = (SELECT job_id
FROM employees
Updating Two Columns WHERE employee_id = 205),
8
with a Subquery salary = (SELECT salary
FROM employees
WHERE employee_id = 205)
WHERE employee_id = 114;
UPDATE copy_emp
SET department_id = (SELECT department_id
FROM employees
Updating Rows Based
9 WHERE employee_id = 100)
on Another Table
WHERE job_id = (SELECT job_id
FROM employees
WHERE employee_id = 200);
Updating Rows: UPDATE employees
10 Integrity Constraint SET department_id = 55
Error WHERE department_id = 110;
DELETE FROM departments
Deleting Rows from a WHERE department_name = 'Finance';
11
Table
DELETE FROM copy_emp;
DELETE FROM employees
WHERE department_id =
Deleting Rows Based
12 (SELECT department_id
on Another Table
FROM departments
WHERE department_name LIKE '%Public%');
Deleting Rows: DELETE FROM departments
13 Integrity Constraint WHERE department_id = 60;
Error
INSERT INTO
Using a Subquery in an
14 (SELECT employee_id, last_name,
INSERT Statement
email, hire_date, job_id, salary,
department_id
FROM employees
WHERE department_id = 50)
VALUES (99999, 'Taylor', 'DTAYLOR',
TO_DATE('07-JUN-99', 'DD-MON-RR'),
'ST_CLERK', 5000, 50);
INSERT INTO departments
(department_id, department_name, manager_id)
Using Explicit Default VALUES (300, 'Engineering', DEFAULT);
15
Values
UPDATE departments
SET manager_id = DEFAULT WHERE department_id = 10;
MERGE INTO copy_emp c
USING employees e
ON (c.employee_id = e.employee_id)
WHEN MATCHED THEN
Merging Rows
UPDATE SET
(Insert or update rows in
c.first_name = e.first_name,
the COPY_EMP table
c.last_name = e.last_name,
16 to match
...
the EMPLOYEES
c.department_id = e.department_id
table.)
WHEN NOT MATCHED THEN
INSERT VALUES(e.employee_id, e.first_name, e.last_name,
e.email, e.phone_number, e.hire_date, e.job_id,
e.salary, e.commission_pct, e.manager_id,
e.department_id);
DELETE FROM employees
WHERE employee_id = 99999;
1 row deleted.
Committing Data
17
INSERT INTO departments
VALUES (290, 'Corporate Tax', NULL, 1700);
1 row inserted.
DELETE FROM copy_emp;
State of the Data After 22 rows deleted.
18
ROLLBACK ROLLBACK;
Rollback complete.
DESCRIBE dept
2 SELECT table_name
FROM user_tables ;
SELECT *
FROM user_catalog ;
9. Including Constraints:
NOT NULL
UNIQUE
PRIMARY KEY
FOREIGN KEY
CHECK
SL Use Statements
CREATE TABLE employees(
employee_id NUMBER(6),
first_name VARCHAR2(20),
1 Defining Constraints ...
job_id VARCHAR2(10) NOT NULL,
CONSTRAINT emp_emp_id_pk
PRIMARY KEY (EMPLOYEE_ID));
CREATE TABLE employees(
employee_id NUMBER(6),
last_name VARCHAR2(25) NOT NULL,
The NOT NULL salary NUMBER(8,2),
2
Constraint commission_pct NUMBER(2,2),
hire_date DATE
CONSTRAINT emp_hire_date_nn
NOT NULL,
CREATE TABLE employees(
employee_id NUMBER(6),
The UNIQUE last_name VARCHAR2(25) NOT NULL,
3
Constraint email VARCHAR2(25),
salary NUMBER(8,2),
commission_pct NUMBER(2,2),
DESCRIBE empvu80;
CREATE VIEW salvu50
Create a view by using AS SELECT employee_id ID_NUMBER, last_name NAME,
2 column aliases in the salary*12 ANN_SALARY
subquery. FROM employees
WHERE department_id = 50;
Retrieving Data from a SELECT *
3
View FROM salvu50;
CREATE OR REPLACE VIEW empvu80
(id_number, name, sal, department_id)
AS SELECT employee_id, first_name || ' ' || last_name,
4 Modifying a View
salary, department_id
FROM employees
WHERE department_id = 80;
CREATE VIEW dept_sum_vu
(name, minsal, maxsal, avgsal)
AS SELECT d.department_name, MIN(e.salary),
Creating a Complex
5 MAX(e.salary),AVG(e.salary)
View
FROM employees e, departments d
WHERE e.department_id = d.department_id
GROUP BY d.department_name;
CREATE OR REPLACE VIEW empvu20
Using the WITH AS SELECT *
6 CHECK OPTION FROM employees
Clause WHERE department_id = 20
WITH CHECK OPTION CONSTRAINT empvu20_ck ;
CREATE OR REPLACE VIEW empvu10
(employee_number, employee_name, job_title)
Denying DML AS SELECT employee_id, last_name, job_id
7
Operations FROM employees
WHERE department_id = 10
WITH READ ONLY;
8 Removing a View DROP VIEW view;
SELECT ROWNUM as RANK, last_name, salary
Example of Top-N FROM (SELECT last_name,salary FROM employees
9
Analysis ORDER BY salary DESC)
WHERE ROWNUM <= 3;
TO scott, manager;
GRANT select, insert
ON departments
TO scott
Using the WITH
WITH GRANT OPTION;
5 GRANT OPTION and
PUBLIC Keywords
GRANT select
ON alice.departments
TO PUBLIC;
Data Dictionary View Description
ROLE_SYS_PRIVS System privileges granted to roles
ROLE_TAB_PRIVS Table privileges granted to roles
USER_ROLE_PRIVS Roles accessible by the user
USER_TAB_PRIVS_MADE Object privileges granted on the user’s
objects
Confirming Privileges USER_TAB_PRIVS_RECD Object privileges granted to the user
6
Granted USER_COL_PRIVS_MADE Object privileges granted on the
columns of the user’s objects
USER_COL_PRIVS_RECD Object privileges granted to the
user on specific columns
USER_SYS_PRIVS Lists system privileges granted to
the user
DECLARE --Optional
--Variables, Cursors, User-defined exceptions
BEGIN --Mandatory
--SQL statements
--PL/SQL statements
EXCEPTION --Optional
--Actions to perform when errors occur
END ; --Mandatory
1. Declaring Variables:
SL Use Statements
SET SERVEROUTPUT ON
DECLARE
v_chuoi VARCHAR2(20);
v_ngay DATE;
BEGIN
v_chuoi := 'Hom nay la ngay :';
v_ngay := Sysdate;
DBMS_OUTPUT.PUT_LINE (v_chuoi||v_ngay);
END;
DECLARE
v_chuoi VARCHAR2(40);
v_ngay DATE;
BEGIN
SELECT last_name||' '||first_name, hire_date
1 Declaring Variables INTO v_chuoi, v_ngay
FROM EMPLOYEES
WHERE employee_id = &ID;
DBMS_OUTPUT.PUT_LINE ('Nhan vien ' ||v_chuoi||' bat dau
lam viec ngay : ' ||v_ngay);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE ('KHONG CO NHAN VIEN
CO');
END;
DECLARE
invalid_so EXCEPTION;
v_so integer := &so;
BEGIN
IF v_so NOT IN (1,2,3) Then
Raise invalid_so;
ELSE
DBMS_OUTPUT.PUT_LINE ('Gia tri : ' ||v_so);
END IF;
EXCEPTION
WHEN Invalid_so THEN
DBMS_OUTPUT.PUT_LINE ('KHONG HOP LE');
END;
(Adopt a naming
convention for
PL/SQL identifiers:
for example,
v_employee_id;)
...
v_name employees.last_name%TYPE;
Declaring Variables
3 v_balance NUMBER(7,2);
with the %TYPE Attribute
v_min_balance v_balance%TYPE := 10;
...
VARIABLE g_salary NUMBER
BEGIN
SELECT salary
INTO :g_salary
4 Using Bind Variables FROM employees
WHERE employee_id = 178;
END;
/
PRINT g_salary
DECLARE
v_sal NUMBER(9,2) := &p_annual_sal;
6 DBMS_OUTPUT.PUT_LINE BEGIN
v_sal := v_sal/12;
DBMS_OUTPUT.PUT_LINE ('The monthly salary is '
||TO_CHAR(v_sal));
END;
/
SL Use Statements
SET SERVEROUTPUT ON
DECLARE
v_deptno NUMBER(4);
v_location_id NUMBER(4);
BEGIN
SELECT department_id, location_id
SELECT Statements in
1 INTO v_deptno, v_location_id
PL/SQL
FROM departments
WHERE department_name = 'Sales';
DBMS_OUTPUT.PUT_LINE (‘Ma phong : ‘ || v_deptno||’ – ‘||
v_location_id);
END;
/
SET SERVEROUTPUT ON
DECLARE
v_hire_date employees.hire_date%TYPE;
v_salary employees.salary%TYPE;
BEGIN
SELECT hire_date, salary
INTO v_hire_date, v_salary
FROM employees
WHERE employee_id = 100;
DBMS_OUTPUT.PUT_LINE (‘Ngay vao lam : ‘ || v_hire_date||
’ va co muc luong :‘ || v_salary);
END;
/
2 Retrieving Data in PL/SQL
SET SERVEROUTPUT ON
DECLARE
v_sum_sal NUMBER(10,2);
v_deptno NUMBER NOT NULL := 60;
BEGIN
SELECT SUM(salary) -- group function
INTO v_sum_sal
FROM employees
WHERE department_id = v_deptno;
DBMS_OUTPUT.PUT_LINE ('The sum salary is ' ||
TO_CHAR(v_sum_sal));
END;
/
BEGIN
INSERT INTO employees
(employee_id, first_name, last_name, email,
hire_date, job_id, salary)
Manipulating Data Using
3 VALUES
PL/SQL
(employees_seq.NEXTVAL, 'Ruth', 'Cores', 'RCORES',
sysdate, 'AD_ASST', 4000);
END;
/
DECLARE
v_sal_increase employees.salary%TYPE := 800;
BEGIN
4 Updating Data
UPDATE employees
SET salary = salary + v_sal_increase
WHERE job_id = 'ST_CLERK';
END;
/
DECLARE
v_deptno employees.department_id%TYPE := 10;
BEGIN
5 Deleting Data DELETE FROM employees
WHERE department_id = v_deptno;
END;
/
DECLARE
v_empno EMPLOYEES.EMPLOYEE_ID%TYPE := 100;
BEGIN
MERGE INTO copy_emp c
USING employees e
ON (e.employee_id = v_empno)
WHEN MATCHED THEN
UPDATE SET
c.first_name = e.first_name,
c.last_name = e.last_name,
c.email = e.email,
c.phone_number = e.phone_number,
Merging Rows
6 c.hire_date = e.hire_date,
c.job_id = e.job_id,
c.salary = e.salary,
c.commission_pct = e.commission_pct,
c.manager_id = e.manager_id,
c.department_id = e.department_id
WHEN NOT MATCHED THEN
INSERT VALUES (e.employee_id, e.first_name, e.last_name,
e.email, e.phone_number, e.hire_date,
e.job_id, e.salary, e.commission_pct,
e.manager_id,e.department_id);
END;
/
VARIABLE rows_deleted VARCHAR2(30)
DECLARE
v_employee_id employees.employee_id%TYPE := 176;
BEGIN
DELETE FROM employees
WHERE employee_id = v_employee_id;
:rows_deleted := ( SQL%ROWCOUNT || ' row deleted.');
END;
/
PRINT rows_deleted
7 SQL Cursor Attributes
CREATE TABLE del_history (
tenbang VARCHAR2(20),
sodong NUMBER(5),
ngayxoa DATE);
:rows_deleted := SQL%ROWCOUNT;
INSERT INTO del_history VALUES ('employees',:rows_deleted,
SYSDATE);
--INSERT INTO del_history VALUES
('employees',SQL%ROWCOUNT, SYSDATE);
END;
/
SELECT * FROM del_history;
DECLARE
v_hire_date DATE := '12-Dec-1990';
v_five_years BOOLEAN;
BEGIN
IF Statements:
...
IF MONTHS_BETWEEN(SYSDATE,v_hire_date)/12 > 5 THEN
IF condition THEN
v_five_years := TRUE;
statements;
ELSE
[ELSIF condition THEN
1 v_five_years := FALSE;
statements;]
END IF;
[ELSE
...
statements;]
END IF;
...
IF v_start > 100 THEN
v_start := 0.2 * v_start;
ELSIF v_start >= 50 THEN
v_start := 0.5 * v_start;
ELSE
v_start := 0.1 * v_start;
END IF;
...
SET SERVEROUTPUT ON
DECLARE
v_grade CHAR(1) := UPPER('&p_grade');
v_appraisal VARCHAR2(20);
BEGIN
v_appraisal := CASE v_grade
WHEN 'A' THEN 'Excellent'
CASE Expressions:
2 WHEN 'B' THEN 'Very Good'
Example
WHEN 'C' THEN 'Good'
ELSE 'No such grade'
END;
DBMS_OUTPUT.PUT_LINE ( 'Grade: '|| v_grade ||
'Appraisal ' || v_appraisal);
END;
/
Example 1:
3 Handling Nulls x := 5;
y := NULL;
...
IF x != y THEN -- yields NULL, not TRUE
sequence_of_statements; -- not executed
END IF;
Example 2:
a := NULL;
b := NULL;
...
IF a = b THEN -- yields NULL, not TRUE
sequence_of_statements; -- not executed
END IF;
SET SERVEROUTPUT ON
DECLARE
v_country_id locations.country_id%TYPE := 'CA';
v_location_id locations.location_id%TYPE;
v_counter NUMBER(2) := 1;
v_city locations.city%TYPE := 'Montreal';
BEGIN
SELECT MAX(location_id) INTO v_location_id FROM locations
WHERE country_id = v_country_id;
4 Basic Loops LOOP
INSERT INTO locations(location_id, city, country_id)
VALUES((v_location_id + v_counter),v_city, v_country_id);
DBMS_OUTPUT.PUT_LINE (‘TRONG : ’ || v_counter);
v_counter := v_counter + 1;
EXIT WHEN v_counter > 3;
END LOOP;
DBMS_OUTPUT.PUT_LINE (‘TRONG : ’ || v_counter);
END;
/
DECLARE
v_country_id locations.country_id%TYPE := 'CA';
v_location_id locations.location_id%TYPE;
v_city locations.city%TYPE := 'Montreal';
v_counter NUMBER := 1;
BEGIN
SELECT MAX(location_id) INTO v_location_id FROM locations
WHERE country_id = v_country_id;
5 WHILE Loops
WHILE v_counter <= 3 LOOP
INSERT INTO locations(location_id, city, country_id)
VALUES((v_location_id + v_counter), v_city,
v_country_id);
v_counter := v_counter + 1;
END LOOP;
END;
/
DECLARE
v_country_id locations.country_id%TYPE := 'CA';
v_location_id locations.location_id%TYPE;
v_city locations.city%TYPE := 'Montreal';
6 FOR Loops
BEGIN
SELECT MAX(location_id) INTO v_location_id
FROM locations
WHERE country_id = v_country_id;
SET SERVEROUTPUT ON
DECLARE
v_myvar NUMBER(2) := 5;
BEGIN
FOR n IN REVERSE 50 .. v_myvar + 50
LOOP
DBMS_OUTPUT.PUT_LINE (‘ FOR 1 : ' || n);
END LOOP;
FOR n IN v_myvar .. v_myvar
LOOP
DBMS_OUTPUT.PUT_LINE (‘FOR 2 : ' || n);
END LOOP;
END;
/
...
BEGIN
<<Outer_loop>>
LOOP
v_counter := v_counter+1;
EXIT WHEN v_counter>10;
<<Inner_loop>>
LOOP
...
7 Nested Loops and Labels EXIT Outer_loop WHEN total_done = 'YES';
-- Leave both loops
EXIT WHEN inner_done = 'YES';
-- Leave inner loop only
...
END LOOP Inner_loop;
...
END LOOP Outer_loop;
END;
SET SERVEROUTPUT ON
DECLARE
TYPE EmpRec IS RECORD (
emp_id employees.employee_id%TYPE,
job_title VARCHAR2(9),
salary NUMBER(7,2));
emp_info EmpRec;
emp_null EmpRec;
emp EmpRec;
BEGIN
emp_info.emp_id := 7788;
emp_info.job_title := 'ANALYST';
emp_info.salary := 3500;
DBMS_OUTPUT.PUT_LINE (‘Ma : ‘||emp_info.emp_id||’-
’||emp_info.job_title||
' - '||emp_info.salary );
emp := emp_info;
DBMS_OUTPUT.PUT_LINE (‘Ma : ‘||emp.emp_id||’ –
‘||emp.job_title ||' - '||emp.salary );
emp := emp_null; -- Tat ca cac field trong emp deu co gia tri null
DBMS_OUTPUT.PUT_LINE (‘Ma : ‘||emp.emp_id||’ -
'||emp.job_title ||' - '||emp.salary );
END;
/
SET SERVEROUTPUT ON
DECLARE
emp_rec employees%ROWTYPE;
BEGIN
SELECT * INTO emp_rec FROM employees
WHERE employee_id = 105;
INSERT INTO e_temp
VALUES (emp_rec.employee_id, emp_rec.first_name,
emp_rec.last_name,
emp_rec.email, emp_rec.phone_number, emp_rec.hire_date,
emp_rec.job_id,
emp_rec.salary, emp_rec.commission_pct, emp_rec.manager_id,
emp_rec.department_id);
COMMIT;
END;
/
SELECT * FROM e_temp;
2 SET SERVEROUTPUT ON
Creating an INDEX BY
DECLARE
Table
TYPE ename_table_type IS TABLE OF
employees.last_name%TYPE
INDEX BY BINARY_INTEGER;
TYPE hiredate_table_type IS TABLE OF DATE
INDEX BY BINARY_INTEGER;
ename_table ename_table_type;
hiredate_table hiredate_table_type;
BEGIN
ename_table(1) := 'CAMERON';
hiredate_table(8) := SYSDATE + 7;
IF ename_table.EXISTS(1) THEN
DBMS_OUTPUT.PUT_LINE (ename_table(1) || ‘ –
‘||hiredate_table(2));
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE (‘Xu ly loi : ‘|| ename_table(1) ||
‘ – ‘ ||hiredate_table(8));
END;
/
SET SERVEROUTPUT ON
DECLARE
TYPE emp_table_type is table of
employees%ROWTYPE INDEX BY BINARY_INTEGER;
my_emp_table emp_table_type;
v_count NUMBER(3):= 10;
BEGIN
FOR i IN 1..v_count
LOOP
SELECT * INTO my_emp_table(i) FROM employees
WHERE employee_id = 100 + i;
END LOOP;
FOR i IN my_emp_table.FIRST..my_emp_table.LAST
LOOP
DBMS_OUTPUT.PUT_LINE(‘Row : ‘||i ||’ –
‘||my_emp_table(i).last_name ||
‘ co muc luong ‘ || my_emp_table(i).salary);
END LOOP;
Example of INDEX BY
3 END;
Table of Records
/
SET SERVEROUTPUT ON
DECLARE
TYPE emp_table_type is table of
employees%ROWTYPE INDEX BY BINARY_INTEGER;
my_table emp_table_type;
v_count NUMBER(3):= 130;
BEGIN
FOR i IN 100..v_count
LOOP
SELECT * INTO my_table(i) FROM employees
WHERE employee_id = i;
END LOOP;
IF my_table.exists(99) THEN
DBMS_OUTPUT.PUT_LINE(‘Khong ton tai trong bang’);
ELSE
DBMS_OUTPUT.PUT_LINE(my_table(106).last_name);
END IF;
END;
/
SET SERVEROUTPUT ON
DECLARE
TYPE emp_table_type is table of
employees%ROWTYPE INDEX BY BINARY_INTEGER;
my_table emp_table_type;
v_count NUMBER(3):= 130;
truoc NUMBER(3):= 1;
sau NUMBER(3):= 1;
BEGIN
FOR i IN 100..v_count LOOP
SELECT * INTO my_table(i) FROM employees WHERE
employee_id = i;
END LOOP;
DBMS_OUTPUT.PUT_LINE('TEST 1: '||my_table.count);
DBMS_OUTPUT.PUT_LINE('Row first: '||my_table.first||' Row
last: '||my_table.last);
sau := my_table.NEXT(125);
truoc := my_table.PRIOR(113);
DBMS_OUTPUT.PUT_LINE('Row prior: '||truoc|| ‘ - Row next :
‘||sau);
END;
SET SERVEROUTPUT ON
DECLARE
TYPE emp_table_type is table of
employees%ROWTYPE INDEX BY BINARY_INTEGER;
my_table emp_table_type;
v_count NUMBER(3):= 130;
truoc NUMBER(3):= 1;
sau NUMBER(3):= 1;
BEGIN
FOR i IN 100..v_count LOOP
SELECT * INTO my_table(i) FROM employees WHERE
employee_id = i;
END LOOP;
DBMS_OUTPUT.PUT_LINE('TEST 1: '||my_table.count);
DBMS_OUTPUT.PUT_LINE('Row first: '||my_table.first||' Row
last: '||my_table.last);
-- my_table.DELETE; -- Xoa tat ca vung nho cap phat cho
my_table
-- my_table.DELETE(102); -- Xoa dong 102
my_table.DELETE(100,120); -- Xoa dong 100 --> 120
DBMS_OUTPUT.PUT_LINE('TEST 2: '||my_table.count);
DBMS_OUTPUT.PUT_LINE ('Row first: '||my_table.first||' Row
last: '||my_table.last);
END;
SET SERVEROUTPUT ON
DECLARE
TYPE CourseList IS TABLE OF VARCHAR2(20);
courses CourseList;
BEGIN
emp_record emp_cursor%ROWTYPE;
BEGIN
OPEN emp_cursor;
LOOP
FETCH emp_cursor INTO emp_record;
EXIT WHEN emp_cursor%NOTFOUND;
INSERT INTO temp_list (empid, empname)
VALUES (emp_record.employee_id, emp_record.last_name);
END LOOP;
COMMIT;
CLOSE emp_cursor;
END;
SET SERVEROUTPUT ON
DECLARE
CURSOR emp_cursor IS SELECT last_name, department_id
FROM employees;
BEGIN
FOR emp_record IN emp_cursor LOOP
--implicit open and implicit fetch occur
5 Cursor FOR Loops IF emp_record.department_id = 80 THEN
DBMS_OUTPUT.PUT_LINE (’Employee ’ ||
emp_record.last_name || ’ works in the Sales
Dept. ’);
END IF;
END LOOP; --implicit close and implicit loop exit
END ;
/
SET SERVEROUTPUT ON
BEGIN
FOR emp_record IN (SELECT last_name, department_id FROM
employees) LOOP
--implicit open and implicit fetch occur
IF emp_record.department_id = 80 THEN
Cursor FOR Loops Using DBMS_OUTPUT.PUT_LINE (’Employee ’ ||
6
Subqueries emp_record.last_name
|| ’ works in the Sales Dept. ’);
END IF;
END LOOP; --implicit close occurs
END ;
/
SET SERVEROUTPUT ON
DECLARE
CURSOR dept_c IS SELECT * FROM departments WHERE
department_id < 60;
CURSOR emp_c (p_deptno NUMBER) IS
SELECT employee_id, last_name FROM employees
WHERE department_id = p_deptno;
dept_r dept_c%rowtype;
emp_r emp_c%rowtype;
BEGIN
OPEN dept_c;
LOOP
FETCH dept_c INTO dept_r;
EXIT WHEN dept_c%NOTFOUND;
DBMS_OUTPUT.PUT_LINE (dept_r.department_id||’
‘||dept_r.department_name);
OPEN emp_c (dept_r.department_id);
LOOP
FETCH emp_c INTO emp_r;
EXIT WHEN emp_c%NOTFOUND;
DBMS_OUTPUT.PUT_LINE (‘NV : ‘||emp_r.employee_id ||
‘-‘||emp_r.last_name);
END LOOP;
CLOSE emp_c;
END LOOP;
CLOSE dept_c;
END;
/
SET SERVEROUTPUT ON
DECLARE
CURSOR dept_c IS SELECT * FROM departments
WHERE department_id < 60;
CURSOR emp_c (p_deptno NUMBER) IS
SELECT employee_id, last_name FROM employees
WHERE department_id = p_deptno;
BEGIN
FOR dept_r IN dept_c
LOOP
DBMS_OUTPUT.PUT_LINE (dept_r.department_id||’ - ‘||
dept_r.department_name);
FOR emp_r IN emp_c (dept_r.department_id)
LOOP
DBMS_OUTPUT.PUT_LINE (‘NV : ‘||emp_r.employee_id
|| ‘-‘||
emp_r.last_name);
END LOOP;
END LOOP;
END;
/
SET SERVEROUTPUT ON
BEGIN
FOR dept_r IN (SELECT * FROM departments
WHERE department_id<60)
LOOP
DBMS_OUTPUT.PUT_LINE (dept_r.department_id||’ - ‘||
dept_r.department_name);
FOR emp_r IN (SELECT employee_id, last_name
FROM employees
WHERE department_id =dept_r.department_id)
LOOP
DBMS_OUTPUT.PUT_LINE (‘NV : ‘||emp_r.employee_id
|| ‘-‘||
emp_r.last_name);
END LOOP;
END LOOP;
END;
/
DECLARE
CURSOR emp_cursor IS
SELECT employee_id, last_name, department_name
FROM employees,departments
2 The FOR UPDATE Clause WHERE employees.department_id =
departments.department_id
AND employees.department_id = 80
FOR UPDATE OF salary NOWAIT;
SET SERVEROUTPUT ON
DECLARE
CURSOR sal_cursor IS
SELECT department_id, employee_id emp_id, last_name, salary
FROM employees WHERE department_id = 20
FOR UPDATE OF salary NOWAIT;
BEGIN
FOR emp_r IN sal_cursor LOOP
DBMS_OUTPUT.PUT_LINE (emp_r.emp_id||’-
’||emp_r.salary);
The WHERE CURRENT
3 IF emp_r.salary > 5000 THEN
OF Clause
UPDATE employees SET salary = emp_r.salary * 1.10
WHERE CURRENT OF sal_cursor;
END IF;
END LOOP;
COMMIT;
END;
/
SELECT department_id, employee_id emp_id, last_name, salary
FROM employees WHERE department_id = 20;
SET SERVEROUTPUT ON
4 Cursors with Subqueries
DECLARE
CURSOR my_cursor IS
SELECT t1.department_id, t1.department_name, t2.staff
FROM departments t1,
(SELECT department_id dept_id, COUNT(*) AS STAFF
FROM employees GROUP BY department_id) t2
WHERE t1.department_id = t2.dept_id AND t2.staff >=
3;
BEGIN
FOR c1 IN my_cursor
LOOP
DBMS_OUTPUT.PUT_LINE (c1.department_name||’-’||
c1.staff);
END LOOP;
END;
/
8. Handling Exceptions :
SL Use Statements
NO_DATA_FOUND
TOO_MANY_ROWS
Sample predefined exceptions:
1 INVALID_CURSOR
ZERO_DIVIDE
DUP_VAL_ON_INDEX
DEFINE p_deptno = 10
DECLARE
e_emps_remaining EXCEPTION;
PRAGMA EXCEPTION_INIT
(e_emps_remaining, -2292);
BEGIN
DELETE FROM departments
2 Nonpredefined Error WHERE department_id = &p_deptno;
COMMIT;
EXCEPTION
WHEN e_emps_remaining THEN
DBMS_OUTPUT.PUT_LINE ('Cannot remove dept ' ||
TO_CHAR(&p_deptno) || '. Employees exist. ');
END;
/
DECLARE
v_error_code NUMBER;
v_error_message VARCHAR2(255);
BEGIN
...
EXCEPTION
...
Functions for Trapping
3 WHEN OTHERS THEN
Exceptions
ROLLBACK;
v_error_code := SQLCODE ;
v_error_message := SQLERRM ;
INSERT INTO errors
VALUES(v_error_code, v_error_message);
END;
/
4 RAISE_APPLICATION_ERROR set serveroutput on
DECLARE
e_name EXCEPTION;
PRAGMA EXCEPTION_INIT (e_name, -20999);
--Executable section :
BEGIN
DELETE FROM employees
WHERE last_name = 'Higginss';
IF SQL%NOTFOUND THEN
RAISE_APPLICATION_ERROR(-20999,'This is not a
valid last name');
END IF;
-- Exception section :
EXCEPTION
WHEN e_name THEN
dbms_output.put_line('handle the error');
END;
/
9. Creating Procedures:
SL Use Statements
CREATE TABLE T1_kiemtra (thongbao varchar2(200));
DECLARE
v_ename employees.last_name%TYPE;
v_sal employees.salary%TYPE := &p_sal;
BEGIN
SELECT last_name INTO v_ename FROM employees WHERE
salary = v_sal;
INSERT INTO T1_kiemtra (thongbao) VALUES (v_ename || ' -
' || v_sal);
EXCEPTION
WHEN no_data_found THEN
INSERT INTO T1_kiemtra (thongbao)
VALUES ('No employee with a salary of '|| TO_CHAR(v_sal));
WHEN too_many_rows THEN
INSERT INTO T1_kiemtra (thongbao)
VALUES ('More than one employee with a salary of '||
Block Structure for
TO_CHAR(v_sal));
Anonymous
1 WHEN others THEN
PL/SQL Blocks
INSERT INTO T1_kiemtra (thongbao)
VALUES ('Some other error occurred.');
END;
/
SELECT * FROM T1_kiemtra;
COMMIT;
END INS_TEST;
/
DECLARE
v_id NUMBER := 163;
BEGIN
Invoking a Procedure from leave_emp2 (v_id); --invoke procedure
7 an Anonymous COMMIT;
PL/SQL Block ...
END;
/
BEGIN
DBMS_OUTPUT.PUT_LINE('Main Procedure p1_ins_loc');
INSERT INTO locations (location_id, city) VALUES (p_lid,
p_city);
SELECT city INTO v_city FROM locations WHERE location_id =
p_lid;
DBMS_OUTPUT.PUT_LINE('Inserted city '||v_city);
DBMS_OUTPUT.PUT_LINE('Invoking the procedure p2_ins_dept
...');
p2_ins_dept(p_lid);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE ('No such dept / loc for any
employee');
END;
/
END q_job;
/
SHOW ERRORS
VARIABLE g_title VARCHAR2(30)
EXECUTE :g_title := q_job (’SA_REP’)
PRINT g_title
BEGIN
IF valid_deptid(p_deptid) THEN
INSERT INTO employees(employee_id, last_name, first_name,
email,
job_id,manager_id, hire_date, salary, commission_pct,
department_id)
VALUES (employees_seq.NEXTVAL, p_lname, p_fname,
p_email,
p_job, p_mgr, TRUNC (SYSDATE, ’DD’), p_sal, p_comm,
p_deptid);
ELSE
RAISE_APPLICATION_ERROR (-20204,’Invalid department ID.
Try again.’);
END IF;
END new_emp;
/
SL Use Statements
SELECT object_name, object_type
List All Procedures and FROM user_objects
1
Functions WHERE object_type in ('PROCEDURE','FUNCTION')
ORDER BY object_name;
SELECT text
List the Code of
FROM user_source
2 Procedures
WHERE name = 'QUERY_EMPLOYEE'
and Functions
ORDER BY line;
CREATE OR REPLACE PROCEDURE log_execution
IS
BEGIN
INPUT INTO log_table (user_id, log_date)
-- wrong
VALUES (USER, SYSDATE);
Detecting Compilation END;
3
Errors: Example /
END over_pack;
/
PROCEDURE proc4_6rows IS
BEGIN
LOOP
FETCH c1 INTO v_empno;
DBMS_OUTPUT.PUT_LINE('Id :' ||(v_empno));
EXIT WHEN c1%ROWCOUNT >= 6;
END LOOP;
CLOSE c1;
END proc4_6rows;
END pack_cur;
/
SET SERVEROUTPUT ON
5 Executing PACK_CUR EXECUTE pack_cur.proc1_3rows
EXECUTE pack_cur.proc4_6rows
CREATE OR REPLACE PACKAGE emp_package IS
TYPE emp_table_type IS TABLE OF employees%ROWTYPE
INDEX BY BINARY_INTEGER;
PROCEDURE read_emp_table (p_emp_table OUT
emp_table_type);
END emp_package;
/
CREATE OR REPLACE PACKAGE BODY emp_package IS
PROCEDURE read_emp_table (p_emp_table OUT
PL/SQL Tables and Records emp_table_type) IS
6
in Packages i BINARY_INTEGER := 0;
BEGIN
FOR emp_record IN (SELECT * FROM employees) LOOP
p_emp_table(i) := emp_record;
i:= i+1;
END LOOP;
END read_emp_table;
END emp_package;
/
SET SERVEROUTPUT ON
DECLARE
v_emp_table emp_package.emp_table_type;
BEGIN
emp_package.read_emp_table(v_emp_table);
FOR i IN 1 .. 5 LOOP
PL/SQL Tables and Records DBMS_OUTPUT.PUT_LINE(‘Nhan vien :
7
in Packages '||v_emp_table(i).last_name ||
‘ co luong : ‘||
v_emp_table(i).salary);
END loop;
END;
/
(TO_CHAR(SYSDATE,'HH24:MI')
NOT BETWEEN '08:00' AND '18:00') THEN
RAISE_APPLICATION_ERROR (-20500,'You
may insert into EMPLOYEES table only
during business hours.');
END IF;
END;
/
KEYBOARD SHORTCUTS :
Model :
SCHEMA SCRIPTS :
-- Schema : ENSOFTBD
commit;
WHEN-NEW-FORMS-INSTANCE : WNFI
PROCEDURE WNFI IS
W NUMBER;
H NUMBER;
W1 NUMBER;
H1 NUMBER;
BEGIN
P_COMPANY;
P_ED('CONTROL.SAVE','D');
IF LTRIM(RTRIM(USER))='ENSOFTBD' THEN
P_ED('CONTROL.DELETE','E');
ELSE
P_ED('CONTROL.DELETE','D');
END IF;
W:=GET_WINDOW_PROPERTY(FORMS_MDI_WINDOW, WIDTH);
H:=GET_WINDOW_PROPERTY(FORMS_MDI_WINDOW, HEIGHT);
W1:= GET_WINDOW_PROPERTY('HRMS', WIDTH);
H1:= GET_WINDOW_PROPERTY('HRMS',HEIGHT);
W:=(W-W1)/2;
H:=(H-H1)/2;
SET_WINDOW_PROPERTY('HRMS', POSITION,W,H);
END;
TRAP ERROR :
PROCEDURE TRAP_ERROR IS
errcode NUMBER := ERROR_CODE;
dbmserrcode NUMBER;
dbmserrtext VARCHAR2(200);
errtxt VARCHAR2(80) := ERROR_TEXT;
errtyp VARCHAR2(3) := ERROR_TYPE;
BEGIN
:MESS :=NULL;
dbmserrcode := DBMS_ERROR_CODE;
dbmserrtext := DBMS_ERROR_TEXT;
:CONTROL.MESS := errtyp||'-'||errcode||'-'||errtxt||'-'||dbmserrcode||'-'||dbmserrtext;
exception
when others then
:CONTROL.MESS := errtyp||'-'||errcode||'-'||errtxt||'-'||dbmserrcode||'-'||dbmserrtext;
END;
PROCEDURE P_SAVE IS
STP CHAR(1);
VEMPID NUMBER(11);
BEGIN
UPDATE ENSOFTBD.PHPMODEL SET
EMPID=EMPID,
EMPNAME=EMPNAME,
DOJ=DOJ,
OFFICE=OFFICE,
GENDER=GENDER,
DESIG=DESIG,
CORRECT=CORRECT,
LUSR=LTRIM(RTRIM(USER)),
LUDT=SYSDATE
WHERE LTRIM(RTRIM(EMPID))=LTRIM(RTRIM(:PHPMODEL.EMPID));
IF SQL%NOTFOUND THEN
P_AUTONO (1,VEMPID);
INSERT INTO ENSOFTBD.PHPMODEL(EMPID, EMPNAME,DOJ, OFFICE
,GENDER,DESIG, CORRECT,OPRTR,INDATE)
VALUES (VEMPID,:PHPMODEL.EMPNAME,:PHPMODEL.DOJ,:PHPMODEL.OFFICE
,:PHPMODEL.GENDER,:PHPMODEL.DESIG,:PHPMODEL.CORRECT,USER,SYSDATE);
STP:='I';
ELSE
STP:='U';
END IF;
FORMS_DDL('COMMIT');
IF FORM_SUCCESS() AND STP='I' THEN
:CONTROL.MESS:='SAVED';
ELSIF FORM_SUCCESS() AND STP='U' THEN
:CONTROL.MESS:='UPDATED';
END IF;
END;
DELETE :
PROCEDURE P_DELETE IS
V_ALERT NUMBER;
BEGIN
if F_CODE_EXISTS(:PHPMODEL.EMPID) then ---- IF EXISTS.
set_alert_property('ALERT_DELETE',ALERT_MESSAGE_TEXT,'Do you really want to delete?');
v_alert:=show_alert('ALERT_DELETE');
IF v_alert =Alert_Button1 Then
DELETE FROM ENSOFTBD.PHPMODEL
WHERE LTRIM(RTRIM(EMPID))=LTRIM(RTRIM(:PHPMODEL.EMPID));
FORMS_DDL('COMMIT');
IF FORM_SUCCESS() THEN
:CONTROL.MESS:='DELETED.';
CLEAR_FORM(NO_VALIDATE);
P_COMPANY;
END IF;
else
:CONTROL.MESS:=NULL;
CLEAR_FORM(NO_VALIDATE);
P_COMPANY;
GO_ITEM('CONTROL.CLEAR');
end if;
ELSE ---- IF NOT EXISTS.
GO_ITEM('CONTROL.CLEAR');
END IF;
END;
VALIDATION :
CLEAR FORM:
CLEAR_FORM(NO_VALIDATE);
SHOW LOV:
SET_ITEM_PROPERTY(ITEM_NAME,ENABLED,PROPERTY_TRUE);
END IF;
ELSIF ED='D' THEN --- DISABLE.
IF GET_ITEM_PROPERTY(ITEM_NAME,ENABLED)='TRUE' THEN
SET_ITEM_PROPERTY(ITEM_NAME,ENABLED,PROPERTY_FALSE);
END IF;
END IF;
END;
GO TO :
GO_TO(‘BLOCK.ITEM’);
DECLARE
PD PARAMLIST;
MZHQ VARCHAR2(2);
BEGIN
IF TO_NUMBER(SUBSTR(LTRIM(RTRIM(:BIZ.BMON3)),1,4)) NOT BETWEEN 2012 AND
TO_NUMBER(TO_CHAR(SYSDATE,'RRRR'))+1 THEN
:BIZ.MESS:='INVALID YEAR GIVEN';
END IF;
IF TO_NUMBER(SUBSTR(LTRIM(RTRIM(:BIZ.BMON3)),5,2)) NOT BETWEEN 1 AND 12
THEN
:BIZ.MESS:='INVALID MONTH GIVEN';
END IF;
IF LENGTH(LTRIM(RTRIM(:BIZ.ZONE)))<>3 OR :BIZ.ZONE IS NULL THEN
:BIZ.MESS:='INVALID ZONE GIVEN';
END IF;
PD :=GET_PARAMETER_LIST('TEST');
IF NOT ID_NULL(PD) THEN
DESTROY_PARAMETER_LIST('TEST');
END IF;
SELECT ZHQ INTO MZHQ FROM PADMA.NEWOFFICE WHERE
LTRIM(RTRIM(ZON))=LTRIM(RTRIM(:ZONE))
AND BRANCH IS NULL
AND ZHQ IS NOT NULL
and rownum=1;
PD :=CREATE_PARAMETER_LIST('TEST');
ADD_PARAMETER(PD, 'P_ZHQ', TEXT_PARAMETER,MZHQ);
ADD_PARAMETER(PD, 'P_ZON', TEXT_PARAMETER,:ZONE);
ADD_PARAMETER(PD, 'P_BMON', TEXT_PARAMETER,:BMON3);
Add_Parameter(PD, 'PARAMFORM', TEXT_PARAMETER, 'no');
Add_Parameter(PD, 'MAXIMIZE', TEXT_PARAMETER, 'Yes');
run_product(reports, :GLOBAL.REPORTS_PATH||'BIZ_ZON.rep', synchronous, runtime, filesystem,PD, null);
END;
Run_Product(REPORTS, :GLOBAL.REPORTS_PATH||'PHPMODEL.REP',SYNCHRONOUS,
RUNTIME,FILESYSTEM, '', NULL);
PROCEDURE PRNT IS
pl_id ParamList;
BEGIN
pl_id := Get_Parameter_List('tmpdata');
IF NOT Id_Null(pl_id) THEN
Destroy_Parameter_List( pl_id );
END IF;
pl_id := Create_Parameter_List('tmpdata');
IF NOT Id_Null(pl_id) THEN
Add_Parameter(pl_id,'number_of_copies',TEXT_PARAMETER,'1');
Add_Parameter(pl_id,'DESTYPE',TEXT_PARAMETER,'PRINTER');
Add_Parameter(pl_id,'P_POLICY',TEXT_PARAMETER,LTRIM(RTRIM(:PRBM_POLNO)));
Add_Parameter(pl_id, 'PARAMFORM', TEXT_PARAMETER, 'NO');
Run_Product(REPORTS, :GLOBAL.REPORTS_PATH||'GFPR.REP',
SYNCHRONOUS, RUNTIME,FILESYSTEM, pl_id, NULL);
END IF;
END;
P_ADD:
:CONTROL.STP:='I';
:CONTROL.MESS:=NULL;
GO_ITEM('PHPMODEL.EMPNAME');
P_EDIT:
:CONTROL.STP:='U';
:CONTROL.MESS:=NULL;
GO_ITEM('PHPMODEL.EMPID');
P_CLEAR:
CLEAR_FORM(NO_VALIDATE);
P_COMPANY;
GO_ITEM('CONTROL.ADD');
DECLARE
pitm varchar2(80);
pitp varchar2(80);
BEGIN
pitm:=ltrim(rtrim(:pitm));
if pitm is not null then
pitp:=Get_Item_Property(pitm,item_type);
end if;
if pitm is not null and pitp='TEXT ITEM' then
Set_Item_Property(pitm,VISUAL_ATTRIBUTE,'v3');
end if;
pitm:=:System.Cursor_Item;
if pitm is not null then
pitp:=Get_Item_Property(pitm,item_type);
end if;
if pitm is not null and pitp='TEXT ITEM' then
Set_Item_Property(pitm,VISUAL_ATTRIBUTE,'v4');
:pitm:=Get_Item_Property(pitm,item_name);
end if;
--F_PROP;
END;
BACKUPS :
PROCEDURE P_BK IS
MPSSWRD VARCHAR2(50);
CURSOR C1 IS SELECT EXPPATH,SUPER,PASSWD,DRIVE FROM
ENSOFTBD.BACKPATH WHERE ID=1;
R1 C1%ROWTYPE;
MBK VARCHAR2(500);
DB VARCHAR2(200);
BEGIN
DB:=GET_APPLICATION_PROPERTY(CONNECT_STRING);
OPEN C1;
FETCH C1 INTO :EXPPATH,:SUPER,:PASSWD,:DRIVE;
CLOSE C1;
mbk:=LTRIM(RTRIM(:EXPPATH))||'\EXP.EXE '||:SUPER||'/'||:PASSWD||'@'||DB||'
FILE='||:DRIVE||':\HRMS\BACKUP\FBACKUP\'||LTRIM(RTRIM(:SUPER))||TO_CHAR(SYSDATE,'RRRRMMDD
-HH24MISS')||'.DMP FULL=N'||' LOG='||:DRIVE||':\HRMS\BACKUP\FBACKUP\'||LTRIM(RTRIM(:SUPER))||'
STATISTICS=NONE';
Host(MBK,NO_PROMPT);
END;
PROCEDURE P_RAR IS
RAR VARCHAR2(200);
BEGIN
--C:\Progra~1\WinRAR\WINRAR.EXE a -r -m3 E:\S0025\S0025NOM
E:\BACKTXT\*NOM.SQL
RAR:='C:\Progra~1\WinRAR\WINRAR.EXE a -r -m3
'||:BACKSYS.DRIVE||':\BACKUP\CBACKUP\BK'||TO_CHAR(SYSDATE,'RRRRMMDD-HH24MISS');
RAR:=RAR||' '||:BACKSYS.DRIVE||':\BACKUP\FBACKUP\';
--------------------------------
synchronize;
Host(RAR,NO_PROMPT);
SYNCHRONIZE;
END;
PROCEDURE P_CREATE_FOLDER IS
m varchar2(100);
BK varchar2(100);
mDrv varchar2(3):=:DRIVE||':\';
l_hostcommand varchar2(1000);
begin
m:='MKDIR '||mDrv||'BACKUP';
BK:=M||'\';
l_hostcommand := 'cmd /c if not exist '||m||' exit 100';
host(l_hostcommand, no_screen);
if not form_success then
HOST(M,NO_PROMPT);
end if;
--------------------------------------------------
m:='MKDIR '||BK||'FBACKUP';
:BACKSYS.FBACKUP:=M;
l_hostcommand := 'cmd /c if not exist '||m||' exit 100';
host(l_hostcommand, no_screen);
if not form_success then
HOST(M,NO_PROMPT);
end if;
--------------------------------------------------
m:='MKDIR '||BK||'CBACKUP';
:BACKSYS.CBACKUP:=M;
l_hostcommand := 'cmd /c if not exist '||m||' exit 100';
host(l_hostcommand, no_screen);
if not form_success then
HOST(M,NO_PROMPT);
end if;
--------------------------------------------------
m:='MKDIR '||BK||'HBACKUP';
:BACKSYS.HBACKUP:=M;
l_hostcommand := 'cmd /c if not exist '||m||' exit 100';
host(l_hostcommand, no_screen);
if not form_success then
HOST(M,NO_PROMPT);
end if;
--------------------------------------------------
END;
ADD_LIST_ELEMENT(MCODE,CNT,R_DESIG.NAME,R_DESIG.CODE);
CNT := CNT + 1;
END LOOP;
IF (:SYSTEM.LAST_RECORD = 'TRUE') THEN
FIRST_RECORD;
END IF;
END;
LOGOUT;
LOGON('HR',MPASS||'@'||MDB); ‘HR’=USERNAME, MPASS=PASSWORD, MDB= DATABASE
VU:=GET_APPLICATION_PROPERTY(USERNAME);
VP:=GET_APPLICATION_PROPERTY(PASSWORD);
VD:=GET_APPLICATION_PROPERTY(CONNECT_STRING);
MENUS:
REPLACE_MENU(:GLOBAL.MENUS_PATH||'MAIN_MENU_ONLINE_SMALL.MMX');
MENU TOGGLE:
OPEN FORMS :
OPEN_FORM(:GLOBAL.FORMS_PATH||'BIZ_SHORT.fmx');
BEGIN
IF LENGTH(LTRIM(RTRIM(F_VAL)))>10 THEN
:MESS:=F_VAL;
ELSE
P_SAVE;
END IF;
END;
PROCEDURE P_MAIN IS
W NUMBER;
H NUMBER;
W1 NUMBER;
H1 NUMBER;
VPATH VARCHAR2(100);
BEGIN
SELECT PATH INTO VPATH FROM ENSOFTBD.PATH WHERE ID=1;
:GLOBAL.FORMS_PATH:=VPATH||'FORMS\';
:GLOBAL.REPORTS_PATH:=VPATH||'REPORTS\';
:GLOBAL.MENUS_PATH:=VPATH||'MENUS\';
REPLACE_MENU(:GLOBAL.MENUS_PATH||'MODEL_MENU.MMX');
-----------------------------------------------------------
SET_WINDOW_PROPERTY(forms_mdi_window, TITLE,'HRMS');
SET_WINDOW_PROPERTY('HRMS', window_state, maximize);
SET_WINDOW_PROPERTY(forms_mdi_window, window_state, maximize);
W:=GET_WINDOW_PROPERTY(forms_mdi_window, width);
H:=GET_WINDOW_PROPERTY(forms_mdi_window, height);
W:=W1;
H:=H1;
IF H>H1+50 THEN
SET_WINDOW_PROPERTY('HRMS', width,W1);
SET_WINDOW_PROPERTY('HRMS', height,H1);
W:=(W-W1)/2;
H:=(H-H1)/2;
SET_WINDOW_PROPERTY('HRMS', POSITION, W,H);
ELSE
SET_WINDOW_PROPERTY('HRMS', window_state, maximize);
END IF;
END;
TOGGLE_INVISIBLE('COLLECTION_MENU.AREAR_COLLECTION');
END IF;
IF MROLE='06' THEN --06-CLAIMS/SB/SURRENDER/MATURITY
TOGGLE_INVISIBLE('POLICY_MENU.SUMMARY');
TOGGLE_INVISIBLE('POLICY_MENU.ALTERATION');
TOGGLE_INVISIBLE('POLICY_MENU.POLICY_ENTRY_OLD');
TOGGLE_INVISIBLE('POLICY_MENU.PRBM_HANDLING');
TOGGLE_INVISIBLE('MENU1.COLLECTION');
TOGGLE_INVISIBLE('HELP_MENU.BANK_BRANCH_ENTRY');
TOGGLE_INVISIBLE('HELP_MENU.PLANS');
TOGGLE_INVISIBLE('HELP_MENU.RATE');
TOGGLE_INVISIBLE('HELP_MENU.SESSION_ENTER');
END IF;
END;
Browse Files :
DECLARE
filename VARCHAR2(256);
BEGIN
filename := GET_FILE_NAME(File_Filter=> 'DMP Files (*.DMP)|*.DMP|');
:fl.ff:=LTRIM(RTRIM(filename));
END;
PROCEDURE P_FORMS IS
filename VARCHAR2(256);
BEGIN
filename := GET_FILE_NAME(File_Filter=> 'FMX Files (*.FMX)|*.FMX|');
:fl.ff:=LTRIM(RTRIM(filename));
END;
PROCEDURE P_REPORTS IS
filename VARCHAR2(256);
BEGIN
filename := GET_FILE_NAME(File_Filter=> 'REP Files (*.REP)|*.REP|');
:fl.rr:=LTRIM(RTRIM(filename));
END;
SET_REPORT_OBJECT_PROPERTY(repid,REPORT_EXECUTION_MODE,BATCH);
SET_REPORT_OBJECT_PROPERTY(repid,REPORT_COMM_MODE,SYNCHRONOU
S);
SET_REPORT_OBJECT_PROPERTY(repid,REPORT_FILENAME,RPT_FILE);
SET_REPORT_OBJECT_PROPERTY(repid,REPORT_DESTYPE,CACHE);
SET_REPORT_OBJECT_PROPERTY(repid,REPORT_DESFORMAT,'PDF');
--SET_REPORT_OBJECT_PROPERTY(repid,REPORT_DESFORMAT,'html');
SET_REPORT_OBJECT_PROPERTY(repid,REPORT_SERVER,rep_svr);
-- for edit -------------
-- SET_REPORT_OBJECT_PROPERTY(repid,REPORT_OTHER,'paramform=no
sdate='||:BLOCK3.STARTDATE||' edate='||:BLOCK3.ENDDATE||' messid='||:BLOCK3.FMESSID);
---SET_REPORT_OBJECT_PROPERTY(repid,REPORT_OTHER,'paramform=no
EMP_ID='||QT||:BLOCK2.ID||QT||' LOAN_DATE='||QT||:BLOCK2.LOAN_DT||QT);
SET_REPORT_OBJECT_PROPERTY(repid,REPORT_OTHER,'paramform=no
P_OFFICE='||:BLOCK3.OFFICE);
v_rep := RUN_REPORT_OBJECT(repid);
v_rep_status := REPORT_OBJECT_STATUS(v_rep);
---message(v_rep_status);
WHILE v_rep_status in ('RUNNING','OPENING_REPORT','ENQUEUED') LOOP
v_rep_status := report_object_status(v_rep);
END LOOP;
IF v_rep_status = 'FINISHED' THEN
/*Display report in the browser*/
v_doc := '/reports/rwservlet/getjobid'|| substr(v_rep,instr(v_rep,'_',-
1)+1)||'?'||'server='||rep_svr;
WEB.SHOW_DOCUMENT(v_doc,'_blank');
ELSE
MESSAGE('Error when running report');
MESSAGE(' ');
END IF;
END;
Calling :
RUN_REPORT('D:\HRMS10g\REPORTS\PHPMODEL_desig.rep','myrpserv');
Oracle 10g Reports Call From Forms without Parameter (From Menu etc.):
DECLARE
rep_url varchar2(2000);
BEGIN
:GLOBAL.REPORTS_PATH:='D:\HRMS10g\REPORTS\';
---
rep_url:='/reports/rwservlet?'||'&report=D:\HRMS10g\REPORTS\PHPMODEL_desig.rep'||'&desformat=ht
mlcss&destype=cache'||'&userid=ensoftbd/e@bappy paramform=yes';
rep_url:='/reports/rwservlet?'||'&report='||:GLOBAL.REPORTS_PATH||'PHPMODEL_desig.jsp'||'&desfor
mat=pdf&destype=cache'||'&userid=ensoftbd/e@bappy paramform=no p_office='||:block3.office;
WEB.SHOW_DOCUMENT(rep_url,'_blank');
END;
For Reports :
(win-7):
set ORACLE_HOME=G:\DevSuiteHome_1
set
PATH=%ORACLE_HOME%\bin;%ORACLE_HOME%\jdk\jre\bin;%ORACLE_HOM
E%\jdk\jre\bin;%ORACLE_HOME%\jdk\jre\bin;%ORACLE_HOME%\jlib;%ORACLE
_HOME%\jdk\bin
(Win-XP):
set ORACLE_HOME=D:\DevSuite10g
set
PATH=%ORACLE_HOME%\bin;%ORACLE_HOME%\jdk\jre\bin;%ORACLE_HOME%\jdk\jre\bin;%
ORACLE_HOME%\jdk\jre\bin;%ORACLE_HOME%\jlib;%ORACLE_HOME%\jdk\bin
D:\DevSuite10g\BIN\rwserver server=myrpserv autostart=yes
For Forms :
For Reports :
(win-7):
set ORACLE_HOME=G:\DevSuiteHome_1
set
PATH=%ORACLE_HOME%\bin;%ORACLE_HOME%\jdk\jre\bin;%ORACLE_HOM
E%\jdk\jre\bin;%ORACLE_HOME%\jdk\jre\bin;%ORACLE_HOME%\jlib;%ORACLE
_HOME%\jdk\bin
(Win-XP):
SET_REPORT_OBJECT_PROPERTY(repid,REPORT_EXECUTION_MODE,BATCH);
SET_REPORT_OBJECT_PROPERTY(repid,REPORT_COMM_MODE,SYNCHRONOU
S);
SET_REPORT_OBJECT_PROPERTY(repid,REPORT_FILENAME,RPT_FILE);
SET_REPORT_OBJECT_PROPERTY(repid,REPORT_DESTYPE,CACHE);
SET_REPORT_OBJECT_PROPERTY(repid,REPORT_DESFORMAT,'PDF');
--SET_REPORT_OBJECT_PROPERTY(repid,REPORT_DESFORMAT,'html');
SET_REPORT_OBJECT_PROPERTY(repid,REPORT_SERVER,rep_svr);
-- for edit -------------
-- SET_REPORT_OBJECT_PROPERTY(repid,REPORT_OTHER,'paramform=no
sdate='||:BLOCK3.STARTDATE||' edate='||:BLOCK3.ENDDATE||' messid='||:BLOCK3.FMESSID);
---SET_REPORT_OBJECT_PROPERTY(repid,REPORT_OTHER,'paramform=no
EMP_ID='||QT||:BLOCK2.ID||QT||' LOAN_DATE='||QT||:BLOCK2.LOAN_DT||QT);
SET_REPORT_OBJECT_PROPERTY(repid,REPORT_OTHER,'paramform=no
P_OFFICE='||:BLOCK3.OFFICE);
v_rep := RUN_REPORT_OBJECT(repid);
v_rep_status := REPORT_OBJECT_STATUS(v_rep);
---message(v_rep_status);
WHILE v_rep_status in ('RUNNING','OPENING_REPORT','ENQUEUED') LOOP
v_rep_status := report_object_status(v_rep);
END LOOP;
IF v_rep_status = 'FINISHED' THEN
/*Display report in the browser*/
v_doc := '/reports/rwservlet/getjobid'|| substr(v_rep,instr(v_rep,'_',-
1)+1)||'?'||'server='||rep_svr;
WEB.SHOW_DOCUMENT(v_doc,'_blank');
ELSE
MESSAGE('Error when running report');
MESSAGE(' ');
END IF;
END;
Calling :
RUN_REPORT('D:\HRMS10g\REPORTS\PHPMODEL_desig.rep','myrpserv');
Oracle 10g Reports Call From Forms without Parameter (From Menu etc.):
DECLARE
rep_url varchar2(2000);
BEGIN
:GLOBAL.REPORTS_PATH:='D:\HRMS10g\REPORTS\';
---
rep_url:='/reports/rwservlet?'||'&report=D:\HRMS10g\REPORTS\PHPMODEL_desig.rep'||'&desformat=ht
mlcss&destype=cache'||'&userid=ensoftbd/e@bappy paramform=yes';
rep_url:='/reports/rwservlet?'||'&report='||:GLOBAL.REPORTS_PATH||'PHPMODEL_desig.jsp'||'&desfor
mat=pdf&destype=cache'||'&userid=ensoftbd/e@bappy paramform=no p_office='||:block3.office;
WEB.SHOW_DOCUMENT(rep_url,'_blank');
END;
(Forms):
(Reports):
Step -1:
E:\Ora11GDevSuite\Middleware\asinst_1\bin\opmnctl createcomponent -
adminUsername weblogic33 -adminHost S0020002L -adminPort 7001 -oracleInstance
E:\Ora11GDevSuite\Middleware\asinst_1 -instanceName asinst_3 -componentName
RPRTSRVR33 -componentType ReportsServerComponent
Step-2:
Step -3:
rwserver server=RPRTSRVR33
Step -4:
E:\Ora11GDevSuite\Middleware\asinst_1\config\reports\bin\rwdiag.bat -findall