ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • oracle PL/SQL_변수 선언 및 데이터 타입
    DataBase/oracle 2020. 3. 22. 14:56

     

    1. 변수 생성규칙

       1) 반드시 문자로 시작한다

       2) 문자나 숫자, 특수문자를 포함 할 수 있다

       3) 변수명은 30bytes 이하여야한다

       4) 예약어(키워드)를 사용하면 안된다

     

    2. 변수의 선언은 선언부(declare)에서 선언되고, 초기화도 함께 할 수 있다

       실행부에서 실행될 경우 값이 할당된다

       서브프로그램의 파라미터로도 전달되기도 하며, 서브프로그램의 출력 결과를 저장하기도한다

     

    3. 선언 예

       emp_no number(6,3) : 숫자를 저장하는 변수로 총 6자리를 의미하며, 소숫점 이하 3자리를 의미한다

       emp_name varchar2(5) : 문자를 저장하는 변수로 총 5byte를 저장할 수 있다

       emp_date date : 날짜를 저장하는 변수

     

    4. 데이터 타입

       char : 고정길이 문자형, 기본 최소값 == 1 , 최대 32,767byte를 저장할 수 있다

       varchar2 :  가변길이 문자형, 기본값은 없다, 최대 32,767byte를 저장할 수 있다

       number(전체자리수, 소수점 이하 자리수) : 숫자형, 전체 자리수 범위 : 1~38 ,

                                                              소수점이하 자리수 범위: -84 ~ 127

       binary_double : 부동 소수점 수를 저장, 9byte 필요함

       date : 날짜 및 시간을 저장 , 날짜의 범위 : 4712B.C ~ 9999 A.D

       timestamp : date 타입을 확장, 연도.월.일.시.분.초 및 소수로 표시되는 초단위까지 저장, 기본값 : 6

     

    5. 참조변수

       empNo employees.employee_id %TYPE 

       -- employees 테이블의 employee_id 의 동일한 타입으로 선언

       emp_name employees.first_name%TYPE

       -- employees 테이블의 first_name과 동일한 타입으로 선언

       empRow employees%ROWTYPE

       --- employees 테이블의 모든 컬럼을 한꺼번에 저장하기 위한 변수로 선언

     

     

    (1) employees의 데이터로 employees1 테이블 생성

    1
    2
    3
    create table employees1 as 
    select employee_id, salary, department_id
    from employees;

     

    (2)

       -- 변수를 선언할 때 employees1테이블의 컬럼 타입으로 지정 하고 

       -- 변수에 값을 넣음

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    declare
        empNo       employees1.EMPLOYEE_ID%TYPE ; 
        empSalary   employees1.SALARY%TYPE ;
     
    begin
        select employee_id, salary into empNo , empSalary
        from employees1
        where department_id  = 10;
     
        dbms_output.put_line (empNo || ' '|| empSalary);
    end;
     
    /
     
     
    http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

     

    (3) RowType

       -- rowType은 객체처럼 변수 하나에 데이터를 담아서 꺼내쓴다

       -- 그런데 레코드가 2개이상일 때부터 에러나는 것 같다

       -- 에러  ORA-01422: exact fetch returns more than requested number of rows
                  ORA-06512: at line 4
                  01422. 00000 -  "exact fetch returns more than requested number of rows"
                  *Cause:    The number specified in exact fetch is less than the rows returned.
                  *Action:   Rewrite the query or change number of rows requested           

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    declare
        emp_row employees1%RowType;
    begin
        select * into emp_row
        from employees1
        where employee_id = 100;
     
        dbms_output.put_line(emp_row.employee_id ||' '|| emp_row.salary||' '||emp_row.department_id );
    end;
     
    /
     
     
    http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

     

    (4) RowType 활용

     

       -- 1) row_test 테이블 을 생성하고 카피해서 row_test2 테이블을 생성한다

       -- 2) row_test 테이블에 데이터 insert

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    create table row_test(
        no number,
        name varchar2(20),
        hdate date
    );
     
    create table row_test2 as select * from row_test;
     
    insert into row_test values(1,'아무개',sysdate);
    insert into row_test values(2'홍길동', sysdate);
    insert into row_Test values(3'고길동',sysdate);
     
    commit;
     
     
    http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

     

     

       -- 3) c_rec 변수의 타입을 지정하고 row_test의 모든 값을 넣는다

       -- 4) row_test2 에 c_rec 변수의 값을 넣는다 

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    declare
        c_rec row_test%RowType;
    begin
        select * into c_rec
        from row_test
        where no = 3;
     
        insert into row_Test2 values c_rec;
    end;
     
    /
     
     
    http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

     

    (5) RowType 변수를 활용한 데이터 변경

       -- 1) RowType 변수를 선언하고 값을 넣음

       -- 2) 변수의 name 컬럼 값 변경

       -- 3) row_test2 테이블에 값 넣긔 , row : 한 레코드를 의미 ,, 그러니까 한 레코드를 다 update하겠다!

       -- 4) 확인해보면 변경!

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    declare
        c_rec row_test%RowType;
    begin
        select * into c_rec
        from row_test
        where no = 3;
        
        c_rec.name := '이길동';
        
        update row_Test2 set row = c_rec -- 오호 여기서는 := 안쓰고 그냥 = 사용
        where no = 3;
    end; 
     
    /
     
    select * from row_Test2;
     
     
    http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

     

    'DataBase > oracle' 카테고리의 다른 글

    oracle PL/SQL_tableType 변수  (0) 2020.03.23
    oracle PL/SQl_rowType변수 및 복합변수 활용  (0) 2020.03.22
    oracle PL/SQL_개념  (0) 2020.03.22
    oracle 계층형 쿼리  (0) 2020.03.22
    oracle 시퀀스  (0) 2020.03.22

    댓글

Designed by Tistory.