How to create in memory data structure list in PLSQL - oracle

I am new to plsql programming. I want to create a list of in-memory data-structure which holds list of records like this
name, city, phone
john, New York, +1-88686
john, London, +44-5343
john, Hong Kong, +33-6556565
I want to do something like this
create table EmployeeTab(
name varchar2(20),
city varchar2(20),
phone varchar(20)
);
But, I could not find the correct syntax. I am using Oracle 11g.

If you want you can use a TYPE RECORD.
Example:
type EmployeeTab is record
(name varchar(20),
city varchar(20),
phone varchar(20));
v_EmployeeTab EmployeeTab;
Then you can save yuor data in this way:
v_EmployeeTab.name := '...';
v_EmployeeTab.city := '...';
v_EmployeeTab.phone := '...';
I suggest you to visit:
https://www.tutorialspoint.com/plsql/plsql_records.htm

You can use Index by table
If you want in memory data structure for a particular table then you define like below
TYPE <typename> IS TABLE OF <tablename>%rowtype INDEX BY binary_integer;
<variablename> <typename>; -- declare variable for that type
If you want index by table for your record then you can use recordname%rowtype
if you want index by table for your cursor then you can use cursorname%rowtype
Index by table will be useful to pass set of values to another procedure

Related

Oracle Object Type and Object Table

I have created an object type and a table. I would like to know how select, insert, update and delete operation on it.
create table employee_info (
empid number,
emp_name varchar2(50),
department varchar2(20),
designation varchar2(50),
salary number
);
create type employee_info_obj is object (
empid number,
department varchar2(50),
designation varchar2(50),
salary number
);
create type employee_info_obj_t is
table of employee_info_obj ;
You have only created an object type and an unrelated database table. If you want a database table based on the type, you need to create one:
create table employee_info of employee_info_obj;
While it can be nice in certain programming scenarios to have a type synced to a table, there are some downsides such as it being harder to add columns later, and third party tool support since the object table will not be listed in user_tables but only in user_object_tables and user_all_tables, so I would question the usefulness of this approach.
dbFiddle

Can I have a varray of tuples in Oracle?

In Oracle, I have a table users. Within this table, for every user, I want to be able to store an array of attributes of a user, that is, key-value pairs.
I could create an extra table with three columns—the foreign key pointing to the users table, the key and the value—but I would prefer keeping things simple and don't want to create an additional table for that.
I know that Oracle has varrays, created like this:
CREATE OR REPLACE TYPE example AS VARRAY(20) OF NVARCHAR2(500);
How do I do the same thing, but instead of NVARCHAR2, I would have a tuple (NVARCHAR2(50), NVARCHAR2(200))?
All the resources about Oracle tuples point to either a grouping of columns to be used in a WHERE ... IN clause, or Java-related documentation.
What if you use record ... and not tuple?
DECLARE
TYPE t_myrecord IS RECORD ( field1 varchar2(50), field2 varchar2(200));
TYPE t_myarray IS VARRAY(20) OF t_myrecord;
a_myrecord t_myrecord;
a_myarray t_myarray := t_myarray();
BEGIN
a_myrecord.field1 := 'a';
a_myrecord.field2 := 'b';
a_myarray.extend(19);
a_myarray(1) := a_myrecord;
END;

How to use REF datatype in PL/SQL code or in SQL statements?

In my understanding REF is an datatype which can store reference to an Object.
I have created a type person.
CREATE TYPE person AS OBJECT(
names VARCHAR2(20),
age NUMBER(4, 2)
);
I created a table of it and inserted a row.
create table person_table of person;
insert into person_table values ('Sam', 24);
Can anyone please tell how I can use REF to store the reference of the inserted object in some other table or PL/SQL block or use REF in any SQL statement?
You need to simply use REF in table creation which will store reference of the PERSON type as follows:
CREATE TABLE EMPLOYEE
(PERSON_DETAILS REF PERSON);
and you can insert data into this table using REF keyword by pointing to the PERSON_TABLE which is itself table of PERSON type as follows:
INSERT INTO EMPLOYEE
SELECT REF(P) FROM PERSON_TABLE P WHERE NAMES = 'Sam';

Insertion in Nested Tables on Oracle DB

I am a new learner of PL/SQL databases,A kind of exercise given to apply database on apex.oracle.com with given sequence.Then I have created tables but when it comes to fill tables with the insertion code as follows,Application has given error,Would you mind if I need your assistance
Thanks in Advance,
CREATE TYPE TEMPORAL_VARCHAR AS OBJECT (
VALID_TIME_LOWER_BOUND DATE,
VALID_TIME_UPPER_BOUND DATE,
VALUE_PART VARCHAR2(50)
);
CREATE TYPE TEMPORAL_NUMBER AS OBJECT (
VALID_TIME_LOWER_BOUND DATE,
VALID_TIME_UPPER_BOUND DATE,
VALUE_PART NUMBER );
Time-related attributeshave defined with the code as follows;
CREATE TYPE NAME_TYPE AS TABLE OF TEMPORAL_VARCHAR;
CREATE TYPE ADDRESS_TYPE AS TABLE OF TEMPORAL_VARCHAR;
CREATE TYPE DEPARTMENT_TYPE AS TABLE OF TEMPORAL_VARCHAR;
CREATE TYPE MANAGER_TYPE AS TABLE OF TEMPORAL_VARCHAR;
CREATE TYPE SALARY_TYPE AS TABLE OF TEMPORAL_NUMBER;
CREATE TABLE EMPLOYEE (
SSN NUMBER primary key,
NAME NAME_TYPE,
ADDRESS ADDRESS_TYPE ,
BIRTH_DATE DATE,
MANAGER MANAGER_TYPE ,
DEPARTMENT DEPARTMENT_TYPE,
SALARY SALARY_TYPE
)
NESTED TABLE NAME STORE AS NAME_TABLE,
NESTED TABLE ADDRESS STORE AS ADDRESS_TABLE,
NESTED TABLE MANAGER STORE AS MANAGER_TABLE,
NESTED TABLE DEPARTMENT STORE AS DEPARTMENT_TABLE,
NESTED TABLE SALARY STORE AS SALARY_TABLE
;
And the insertion that I am inteded to do
INSERT INTO EMPLOYEE VALUES
(101,
NAME(TEMPORAL_VARCHAR('23.11.2005','12.31.9999','James Brown')),
ADDRESS(TEMPORAL_VARCHAR('23.11.2005','12.31.9999','BUCA, IZMIR')),
'23.10.1986',
MANAGER(TEMPORAL_VARCHAR('23.11.2005','12.31.9999','Mike White')),
DEPARTMENT(TEMPORAL_VARCHAR('23.11.2005','12.31.9999','DEPT_ID05')),
SALARY(TEMPORAL_NUMBER('23.11.2005',’12.31.9999’, 250000))
);
And the error message I recieved is :
ORA-00904: "SALARY": invalid identifier
There are spaces before _ here
CREATE TYPE MANAGER _TYPE AS TABLE OF TEMPORAL_VARCHAR;
CREATE TYPE SALARY _TYPE AS TABLE OF TEMPORAL_NUMBER;
DEPARTMENT DEPARTMENT _TYPE
A comma is missing after DEPARTMENT _TYPE
try this solution:
'alter session set NLS_DATE_FORMAT='DD.MM.YYYY';'
----------------------------------------------------
INSERT INTO EMPLOYEE VALUES
(101,
NAME_TYPE(TEMPORAL_VARCHAR('23.10.1986','09.09.9999','James Brown')),
ADDRESS_TYPE(TEMPORAL_VARCHAR('15.12.2009','09.09.9999','BUCA')),
'23.10.1986',
MANAGER_TYPE(TEMPORAL_VARCHAR('24.05.2008','09.09.9999','Mike White')),
DEPARTMENT_TYPE(TEMPORAL_VARCHAR('03.01.2012','09.09.9999','DEPT_ID05')),
SALARY_TYPE(TEMPORAL_NUMBER('01.01.2003','09.09.9999', 3200))
);

Oracle Insert Data Into Object Table

What is the most per-formant manner to insert data into an object table?
Create Type R_Emp Is Object
(Emp_Id number, Last_Name varchar(50));
create type T_Emp is table of R_Emp;
Then given an inbound array insert values:
V_T_Emp T_Emp := T_Emp();
For i In 1..p_array.COUNT
Loop
..... // Best way to load values
Where is the data coming from? Assuming the data is coming from one or more tables
SELECT r_emp( emp_id, last_name )
BULK COLLECT INTO v_t_emp
FROM table_name
If the data is coming from somewhere else, you'll need to tell us where the data is coming from. If p_array and v_t_emp are both collections of type t_emp, you can just directly assign them
v_t_emp := p_array;
But if you're just copying the data from one collection to another identically defined collection, there normally wouldn't be any reason to introduce the second collection.

Resources