-
Notifications
You must be signed in to change notification settings - Fork 0
/
records.sql
69 lines (59 loc) · 1.68 KB
/
records.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
-- PL/SQL has three records -> table-based, cursor-based, programmer-defined
-- table-based, from contacts table
DECLARE
r_contact contacts%ROWTYPE;
BEGIN
SELECT first_name, last_name, phone
INTO r_contact
FROM contacts WHERE contact_id = 100;
END;
-- Cursor-based
DECLARE
CURSOR c_contacts IS
SELECT first_name, last_name, phone FROM contacts;
r_contact c_contacts%ROWTYPE;
BEGIN
NULL;
END;
-- Programmer-defined
DECLARE
-- Define record type
TYPE r_customer_contact_t IS RECORD(
customer_name customers.name%TYPE,
first_name contacts.first_name%TYPE,
last_name contacts.last_name%TYPE
);
-- Declare record
r_customer_contacts r_customer_contact_t;
BEGIN
NULL;
END;
-- test case
---------
CREATE TABLE person_records (
person_id NUMBER GENERATED BY DEFAULT AS IDENTITY,
first_name VARCHAR2( 50 ) NOT NULL,
last_name VARCHAR2( 50 ) NOT NULL,
primary key (person_id)
);
DECLARE
r_person person_records%ROWTYPE; --table record
BEGIN
-- assign values to record
r_person.person_id := 1;
r_person.first_name := 'John';
r_person.last_name := 'Doe';
-- insert a new person to table
INSERT INTO person_records VALUES r_person;
END;
-- update %ROWTYPE record
DECLARE
r_person person_records%ROWTYPE;
BEGIN
-- get data of person id 1
SELECT * INTO r_person FROM person_records WHERE person_id = 1;
-- change person's last name
r_person.last_name := 'Smith';
-- update the person
UPDATE person_records SET ROW = r_person WHERE person_id = r_person.person_id;
END;