반응형



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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/* 
    Table Type 변수(컬렉션)
    - 컬렉션 : 일반 프로그래밍 언어에서 사용하는 배열 타입을 PL/SQL에서는 컬렉션이라 한다.
    - 종류
        : 연관배열(associative array / index-by table) : 키와 값의 쌍으로 구성된 컬렉션
                                                       : JAVA의 해시테이블과 같은 개념
            - key 데이터 유형 : 숫자(binary_integer, pls_integer 
                                    - 이 데이터 타입은 number보다 작은 저장영역 필요, 
산술연산의 경우 number보다 빠르다 - )  
                              : 문자 (varchar2)
            - 값(value) 유형: 일반 데이터 타입 사용됨,레코드 데이터 타입사용가능- 
레코드 타입의 경우, 여러개의 값을 가질 수 있다.-
            
        : varray(variable array) : 고정 길이를 가진 배열
                                 : 일반 프로그래밍에서 사용하는 배열과 같다.
        : 중첩테이블(nested table) : varray와 흡사한 구조의 배열
                                   : 배열의 크기를 명시하지 않고, 동적으로 배열의 크기가 설정됨
                                   
    - Table Type 선언 형식
    1. 정의
        Type 타입명 is table of
        employees.first_name%Type
        INDEX BY binary_integer;
    2. 선언 ( 메모리화 - 변수의 공간이 메모리에 설정됨 )
        식별자 타입명;
*/
 
set serveroutput on;
 
declare
    tname varchar2(20);
    
    type t_emp_name is table of
    employees.last_name%type
    index by binary_integer;
    
    v_name t_emp_name;
    
begin
    select last_name into tname
    from employees
    where employee_id = 100;
    
    v_name(0) := tname;
    dbms_output.put_line(v_name(0));
end;
/
 
 
 
    
declare
    type tbl_type is table of 
    employees.last_name%type
    index by binary_integer;
    
    vtbl_type tbl_type;
    a binary_integer := 0;
    
begin
    for emp_name in(select last_name from employees) loop
        a:= a+1;
        vtbl_type(a):= emp_name.last_name;
    end loop;
 
    for i in 1..a loop
        dbms_output.put_line(vtbl_type(i));
    end loop;
 
end;
/
 
 
 
/*  
    바인드 변수 (비 PL/SQL 변수)
    - 호스트환경에서 생성되어 데이터를 저장하기 때문에 호스트 변수라고 한다
    - 키워드 variable을 이용하며, SQL문에서 사용 가능, PL/SQL블럭에서도 사용 가능
    - PL/SQL블록이 실행된 후에도 엑세스가 가능하다
    - print명령을 이용해 출력 가능
    - :을 붙여 이용한다
*/
 
set autoprint on;
 
begin
    select (salary*12 + nvl(commission_pct*salary,0)) into :vsal
    from employees
    where employee_id = 100;
end;
/
 
print vsal;

cs








반응형

'프로그래밍 > SQL' 카테고리의 다른 글

[Oracle SQL] ROWTYPE 변수 및 복합변수 활용  (0) 2017.09.08
[Oracle SQL] PL/SQL 변수 선언 및 데이터 타입  (0) 2017.09.06
[Oracle SQL] PL/SQL  (0) 2017.08.21
[Oracle SQL] 계층형 쿼리  (0) 2017.08.17
[Oracle SQL] Sequence  (0) 2017.08.16

+ Recent posts