본문 바로가기

Database/PL SQL

PL/SQL Tutorial - Record

 PL/SQL record는 multiple fields를 가진다. 구조체로 이해하면 편할 것 같다.

1) Declaring records

DECLARE
   record_name table_name%ROWTYPE;

간단하다. 실제로 contats의 row 구조를 record로 저장하는 경우는 아래와 같다.

DECLARE
   r_contact contacts%ROWTYPE;

2) Cursor-based record

DECLARE
    record_name cursor_name%ROWTYPE;

안봐도 느낌이 커서의 구조를 가져가는 record이다.

DECLARE
    CURSOR cur_contacts IS
        SELECT first_name, last_name, phone
        FROM contacts;
    r_contact cur_contacts%ROWTYPE;

cur_contacts 는 first_name, last_name, phone을 가지고 있으며 r_contact는 cur_contact의 구조를 가진 record로 정의된다.

여태의 레코드들은 정확한 구조를 가진 친구들이다. 하지만 언제나 구조가 존재할 수 없으므로 직접 define 하는 법도 알아두자.

TYPE record_type IS RECORD (
    field_name1 data_type1 [[NOT NULL] := | DEFAULT default_value],
    field_name2 data_type2 [[NOT NULL] := | DEFAULT default_value],
    ...
);

record_name record_type;

DECLARE
  -- define a record type
TYPE customer_contacts
IS
  RECORD
  (
    customer_name customers.name%TYPE,
    first_name contacts.first_name%TYPE,
    last_name contacts.last_name%TYPE );
  -- declare a record
  r_customer_contacts customer_contacts;
BEGIN
  NULL;
END;

레코드의 필드는 레코드 이름.필드 이름으로 사용할 수 있다.

record_name.field_name

예시
r_contact.first_name

같은 타입의 레코드를 만들고 싶을 때에는 대입해주면 된다. 

r_contact1 : = r_contact2;

이게 된다고 레코드들끼리 비교를 할 순 없다

IF r_contact1 = r_contact2 THEN
    ...    
END IF; 

이거 안됨

각각 필드를 비교해서 하는건 가능하다.

IF r.contact1.first_name = r.contact2.first_name AND 
    r.contact1.last_name = r.contact2.last_name AND
    r.contact1.phone = r.contact2.phone THEN
    ...
END IF;

귀찮지만 able

레코드에 값도 집어넣어줄 수 있다.

r_contact.first_name := 'John';
r_contact.last_name := 'Doe';
r_contact.phone := '(408-654-2865)';

SELECT도 가능하고 FETCH도 가능하다.

SELECT
  first_name, last_name, phone
INTO
  r_contact
FROM
  contacts
WHERE
  contact_id = 100;
  
-- fetch a whole record
FETCH cur_contacts INTO r_contact;
 
-- fetch individual fields
FETCH
  cur_contacts
INTO
  r_contact.first_name, 
  r_contact.last_name, 
  r_contact.phone;

중첩해서도 만들 수 있다. ship_to, bill_to는 address record의 구조를 갖는다.

DECLARE
  TYPE address IS RECORD (
    street_name VARCHAR2(255),
    city VARCHAR2(100),
    state VARCHAR2(100),
    postal_code VARCHAR(10),
    country VARCHAR2(100)
  );
  TYPE customer IS RECORD(
      customer_name VARCHAR2(100),
      ship_to address,
      bill_to address
  );
  r_one_time_customer customer;
BEGIN
 
  r_one_time_customer.customer_name := 'John Doe';
  -- assign address
  r_one_time_customer.ship_to.street_name := '4000 North 1st street';
  r_one_time_customer.ship_to.city := 'San Jose';
  r_one_time_customer.ship_to.state := 'CA';
  r_one_time_customer.ship_to.postal_code := '95134';
  r_one_time_customer.ship_to.country := 'USA';
  -- bill-to address is same as ship-to address
  r_one_time_customer.bill_to := one_time_customer.ship_to;
END;

'Database > PL SQL' 카테고리의 다른 글

PL/SQL Tutorial - Variables with REF CURSOR  (0) 2019.05.08
PL/SQL Tutorial - Parameters  (0) 2019.05.08
PL/SQL Tutorial - Cursor FOR LOOP  (0) 2019.05.08
PL/SQL Tutorial - Cursor  (0) 2019.05.08
PL/SQL Tutorial - SELECT INTO  (0) 2019.05.08