Live ORACLE

if you are Oracle Developer ,than this Blog most likely will make you happy.

About my Blog

This Blog particularized for Oracle Developers ... you will see interesting Codes for SQL , PL/SQL as well as new ideas for Developer Suite and Client Tools that will help you in your professional life ... and I hope I reach for your satisfaction.

About Me

I'm Hany Freedom , 25 years old , I live in EL-Minia City in Egypt , I'm Moderator at ArabOUG.org the best Arabic Forum for ORACLE on the Net. if you interested to know more about me .... just Click Here.

Nested Table Constructor Within a SQL Statement

20:46:31 SQL> DROP TABLE sophomores CASCADE CONSTRAINTS PURGE
20:46:32 2 /
DROP TABLE sophomores CASCADE CONSTRAINTS PURGE
*
ERROR at line 1:
ORA-00942: table or view does not exist


Elapsed: 00:00:00.01
20:46:32 SQL> DROP TYPE student
20:46:32 2 /
DROP TYPE student
*
ERROR at line 1:
ORA-04043: object STUDENT does not exist


Elapsed: 00:00:00.00
20:46:32 SQL> DROP TYPE CourseList
20:46:32 2 /
DROP TYPE CourseList
*
ERROR at line 1:
ORA-04043: object COURSELIST does not exist


Elapsed: 00:00:00.00
20:46:32 SQL> CREATE TYPE CourseList AS TABLE OF VARCHAR2(10)
20:46:32 2 /

Type created.

Elapsed: 00:00:00.03
20:46:32 SQL> CREATE TYPE student AS OBJECT (
20:46:32 2 id_num INTEGER(4),
20:46:32 3 name VARCHAR2(25),
20:46:32 4 address VARCHAR2(35),
20:46:32 5 status CHAR(2),
20:46:32 6 courses CourseList)
20:46:32 7 /

Type created.

Elapsed: 00:00:00.06
20:46:32 SQL> CREATE TABLE sophomores of student NESTED TABLE courses STORE AS courses_nt
20:46:32 2 /

Table created.

Elapsed: 00:00:00.04
20:46:32 SQL> BEGIN
20:46:32 2 INSERT INTO sophomores
20:46:32 3 VALUES (5035, 'Janet Alvarez', '122 Broad St', 'FT',
20:46:32 4 CourseList('Econ 2010', 'Acct 3401', 'Mgmt 3100'));
20:46:32 5 END;
20:46:32 6 /

PL/SQL procedure successfully completed.

Elapsed: 00:00:00.01
20:46:33 SQL> SPOOL OFF


Share/Save/Bookmark

Creating a Table with a Varray Column

DROP TABLE dept_projects CASCADE CONSTRAINTS PURGE;
DROP type projectlist;

-- Each project has a 16-character code name.
-- We will store up to 50 projects at a time in a database column.
CREATE TYPE projectlist AS VARRAY(50) OF VARCHAR2(16);
/

CREATE TABLE dept_projects ( -- create database table
dept_id NUMBER(2),
NAME VARCHAR2(15),
budget NUMBER(11,2),
-- Each department can have up to 50 projects.
projects PROJECTLIST);


Varray Constructor Within a SQL Statement

BEGIN
INSERT INTO dept_projects
VALUES (60,
'Security',
750400,
Projectlist('New Badges','Track Computers','Check Exits'));
END;
/


Share/Save/Bookmark

Declaring a Nested Table in SQL

DROP TABLE sophomores CASCADE CONSTRAINTS PURGE;
DROP TYPE student;
DROP TYPE CourseList;
-------------------------------------------
CREATE TYPE CourseList AS TABLE OF VARCHAR2(10) -- define type
/
CREATE TYPE student AS OBJECT ( -- create object
id_num INTEGER(4),
name VARCHAR2(25),
address VARCHAR2(35),
status CHAR(2),
courses CourseList); -- declare nested table as attribute
/
CREATE TABLE sophomores of student
NESTED TABLE courses STORE AS courses_nt
/



OUTPUT:-

20:40:43 SQL> DROP TABLE sophomores CASCADE CONSTRAINTS PURGE;

Table dropped.

Elapsed: 00:00:00.04
20:40:44 SQL> DROP TYPE student;

Type dropped.

Elapsed: 00:00:00.09
20:40:44 SQL> DROP TYPE CourseList;

Type dropped.

Elapsed: 00:00:00.06

20:40:44 SQL> CREATE TYPE CourseList AS TABLE OF VARCHAR2(10) -- define type
20:40:44 2 /

Type created.

Elapsed: 00:00:00.01
20:40:44 SQL> CREATE TYPE student AS OBJECT ( -- create object
20:40:44 2 id_num INTEGER(4),
20:40:44 3 name VARCHAR2(25),
20:40:44 4 address VARCHAR2(35),
20:40:44 5 status CHAR(2),
20:40:44 6 courses CourseList); -- declare nested table as attribute
20:40:44 7 /

Type created.

Elapsed: 00:00:00.06
20:40:44 SQL> CREATE TABLE sophomores of student
20:40:44 2 NESTED TABLE courses STORE AS courses_nt
20:40:44 3 /

Table created.

Elapsed: 00:00:00.03

Share/Save/Bookmark

Using DEREF in a SELECT INTO Statement

DECLARE
emp EMPLOYEE_TYP;
emp_ref REF EMPLOYEE_TYP;
emp_name VARCHAR2(50);
BEGIN
SELECT Ref(e)
INTO emp_ref
FROM employee_tab e
WHERE e.employee_id = 370;
-- the following assignment raises an error, not allowed in PL/SQL
-- emp_name := emp_ref.first_name || ' ' || emp_ref.last_name;
-- emp := DEREF(emp_ref); not allowed, cannot use DEREF in procedural statements
SELECT Deref(emp_ref)
INTO emp
FROM dual; -- use dummy table DUAL
emp_name := emp.first_name
||' '
||emp.last_name;
dbms_output.Put_line(emp_name);
END;
/


Share/Save/Bookmark

Updating Rows in an Object Table With a REF Modifier

DECLARE
emp EMPLOYEE_TYP;
emp_ref REF EMPLOYEE_TYP;
BEGIN
SELECT Ref(e)
INTO emp_ref
FROM employee_tab e
WHERE e.employee_id = 370;
UPDATE employee_tab e
SET e.address = Address_typ('8701 College','Oakland','CA','94321')
WHERE Ref(e) = emp_ref;
END;
/


Share/Save/Bookmark

Updating and Deleting Rows in an Object Table

DECLARE
emp EMPLOYEE_TYP;
BEGIN
INSERT INTO employee_tab
VALUES (Employee_typ(370,'Robert','Myers','RMYERS','555.111.2277',
'07-NOV-04','SA_REP',8800,.12,101,110,Address_typ('540 Fillmore','San Francisco','CA','94011')));
UPDATE employee_tab e
SET e.address.street = '1040 California'
WHERE e.employee_id = 370;
DELETE FROM employee_tab e
WHERE e.employee_id = 310;
END;
/


Share/Save/Bookmark

Inserting Rows in an Object Table

DECLARE
emp EMPLOYEE_TYP;
BEGIN
INSERT INTO employee_tab
VALUES (Employee_typ(310,'Evers','Boston','EBOSTON','555.111.2222',
'01-AUG-04','SA_REP',9000,.15,101,110,Address_typ('123 Main','San Francisco','CA','94111')));
INSERT INTO employee_tab
VALUES (Employee_typ(320,'Martha','Dunn','MDUNN','555.111.3333',
'30-SEP-04','AC_MGR',12500,0,101,110,Address_typ('123 Broadway','Redwood City','CA','94065')));
END;
/


Share/Save/Bookmark

Accessing Object Attributes

DECLARE
emp EMPLOYEE_TYP;
BEGIN
emp := Employee_typ(315,'Francis','Logan','FLOGAN','555.777.2222',
'01-MAY-04','SA_MAN',11000,.15,101,110,Address_typ('376 Mission','San Francisco','CA','94222'));
dbms_output.Put_line(emp.first_name
||' '
||emp.last_name);
dbms_output.Put_line(emp.address.street);
dbms_output.Put_line(emp.address.city
||', '
||emp.address.state
||' '
||emp.address.postal_code);
END;
/



OUTPUT:-

Francis Logan
376 Mission
San Francisco, CA 94222

PL/SQL procedure successfully completed.

Share/Save/Bookmark
Newer Posts Older Posts Home Page
 
http://www.dpriver.com/images/sqlpp-banner-2.png

Thanks for reading my Blog ... you Visitor Number :-