-
oracle PL/SQL_tableType 변수DataBase/oracle 2020. 3. 23. 01:30
Table Type 변수 (=컬렉션)
컬렉션 : 일반 프로그래밍 언어에서 사용하는 배열타입을 PL/SQL에서는 컬렉션이라고한다!
1. 종류
(1) 연관배열 ( associative array / index-by table ) : key 와 value 를 같이 갖고있는 것
1) key의 데이터 유형 :
-->숫자 : binary_integer, pls_integer ( 두 데이터타입은 number보다 작은 저장영역이 필요
산술 연산의 경우 number보다 빠르다
--> 문자 : varchar2
2) value의 데이터 유형 : 일반 데이터 타입, 레코드(=여러개의 값을 가질 수 있다)
(2) varray (=variable array) : 고정길이를 가진 배열, 일반 프로그래밍에서 사용하는 배열과 같다,
순서O, 0부터 시작
(3) 중첩테이블 (=nested table) : 배열의 크기를 명시하지않고 동적으로 배열의 크기가 결정됨, 순서X,
2. Table Type 선언형식
1. 정의
Type 타입명 IS TABLE OF
ex) employees.first_name%type
INDEX BY BINARY_INTEGER;
2. 선언 (= 메모리화)
식별자 타입명;
1234567891011121314151617181920212223242526declaretname varchar2(20);--정의Type t_emp_name is table ofemployees.last_name%typeindex by binary_integer; --배열형식이므로 index가 필요한데 binary_integer타입의 index이다--선언v_name t_emp_name; -- v_name 은 테이블타입의 변수beginselect last_name into tnamefrom employeeswhere employee_id = 100;v_name(0) := tname;dbms_output.put_line(v_name(0));end;/http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter반복문을 이용하여 배열 출력
1234567891011121314151617181920212223242526272829declaretype tbl_type is table ofemployees.last_name%typeindex by binary_integer;vtbl_type tbl_type;--binary_integer 값을 0으로 초기화--i 라는 변수의 값을 초기화i binary_integer := 0;beginfor emp_name in (select last_name from employees) loopi:= i+1;vtbl_type(i) := emp_name.last_name;end loop;for j in 1..i loop -- 변수 j를 1부터 i까지 돌리겠다dbms_output.put_line(vtbl_type(j));end loop;end;/http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter-- 바인트 변수 (비 PL/SQL 변수)
-- 호스트 환경에서 생성되어 데이터를 저장하기때문에 호스트변수라고 말한다 <- 변수를 선언하지않고 바로사용가능
-- 키워드 VARIABLE을 이용하며, SQL문이나 PL/SLQ블록에서도 사용가능
-- PL/SQL 블록이 실행된 후에도 엑세스가 가능하다
-- print 명령을 이용하여 출력이 가능하다
-- :을 붙여 이용한다1234567891011121314set autoprint on; -- pl블록 실행후 자동으로 print 실행beginselect (salary*12+nvl(commission_pct*salary,0)) into :vsalfrom employeeswhere employee_id = 100;end;/print vsal; --vsal은 출력되는데 값은 출력 안된다...뭐지http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter'DataBase > oracle' 카테고리의 다른 글
oracle PL/SQL_ 커서(cursor) (0) 2020.03.29 oracle PS/SQL_제어문 (0) 2020.03.28 oracle PL/SQl_rowType변수 및 복합변수 활용 (0) 2020.03.22 oracle PL/SQL_변수 선언 및 데이터 타입 (0) 2020.03.22 oracle PL/SQL_개념 (0) 2020.03.22