ORACLE SQL (9) 썸네일형 리스트형 [Oracle] SQL - interval 데이터타입 활용하기 공부를 하다가 보니 table을 생성할 때, datatype으로 'interval year(3) to month' 'interval day(3) to second' 라고 선언해서 사용하는 것을 보고 이번 포스팅을 하게 되었습니다. 1 2 3 4 5 create table cc( today date, year1 interval year(3) to month, day1 interval day(3) to second ); cs 위처럼 cc 라는 table을 생성하였습니다. 변수이름 : year1 - 데이터타입 : interval year(3) to month 에는 실제 데이터를 insert할때 interval '개월간격' to month(3) interval '연간격' to year(3) 를 넣을 수 있습니다.. [ORACLE] SQL - 프로시저 (매개변수가 1개인 경우, 2개인 경우) *제 자신의 이해를 돕기 위해 프로시저 이름을 무의미하게 만들었음을 미리 알려드립니다.* 프로시저를 생성할때 명령문 실행(ctrl+enter)가 아닌 스크립트 실행(f5)를 사용합니다. 1 2 3 4 5 6 7 8 9 10 11 12 CREATE OR REPLACE procedure hh (f_name in employees.first_name%type) as v_sal number(10); begin select salary into v_sal from employees where first_name = f_name; dbms_output.put_line(f_name||' '||v_sal); end hh; / Colored by Color Scripter cs 아래 execute를 사용하여 실행하면 됩.. [ORACLE] SQL - 스토어드 프로시저(Procedure) 1 2 3 4 5 6 7 8 9 10 11 12 create or replace procedure sp_print5 is num number :=1; begin while(num [ORACLE] SQL 반복문 - FOR문, WHILE문[ORACLE] SQL 반복문 - FOR문, WHILE문 WHILE문 1 2 3 4 5 6 7 8 9 10 11 12 13 declare num number :=1; begin Loop dbms_output.put_line('while:'||num); num := num + 1; if(num>5) then exit; end if; end loop; end; / Colored by Color Scripter cs FOR문 1 2 3 4 5 6 7 8 9 declare begin for i in 1..5 loop dbms_output.put_line('for:'||i); end loop; end; / Colored by Color Scripter cs WHILE 조건문 1 2 3 4 5 6 7 8 9 declare num number :=1; begin while.. [ORACLE] SQL -- if문 예시 12345678910111213141516171819202122declareemp_record2 employees%rowtype;begin SELECT * into emp_record2 FROM employees where employee_id = 101; if(emp_record2.commission_pct is null) then dbms_output.put_line('null이다 '||emp_record2.salary*12); else dbms_output.put_line('null이 아니다 '||emp_record2.salary*12+emp_record2.salary*emp_record2.commission_pct); end if; dbms_output.put_line(emp_record2.emp.. [ORACLE] SQL - TYPE의 is record 활용하기 1. is record 안에 변수명, 타입을 넣어준다. 2. 만든 타입으로 변수(aa)를 만든다. 3. select ~~ into를 이용하여 column의 값들을 aa에 넣는다. 4. aa.해당 변수를 써서 해당 값을 읽어올수있다. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 declare type aa_type is record( v_fname employees.first_name%TYPE, v_lname employees.last_name%TYPE, v_sal employees.salary%TYPE, v_deptno employees.department_id%TYPE ); aa aa_type; begin SELECT first_name,last_name,.. [Oracle] SQL문 - TYPE과 FOR문 활용 예시 1. aa_type이라는 타입을 만듭니다. 2. employees.first_name%type를 써서 원래 employees의 first_name의 타입을 그대로 가져옵니다. 3. index by binary_integer를 붙여 딕셔너리 형식으로 숫자를 넣고 값을 입력할수 있도록 만듭니다. 4. for 문을 활용해서 employees 테이블의 fist_name를 하나씩 읽어오고 이를 bb에 담아서 for문을 돌립니다. 5. idx 는 1씩 추가하도록 만들어 aa(숫자)의 숫자를 하나씩 늘리고 담아온 bb의 first_name을 aa(idx에 담습니다.) 6. for cc를 활용해서 값을 읽어옵니다. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 DECLARE type aa_type i.. [ORACLE] SQL - TRIGGER(DELETE시 데이터 백업 예시) 안녕하세요. TRIGGER를 활용해서 테이블에서 DELETE시, 별도의 TABLE 에 알아서 백업해주는 코드입니다. 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 --ABCD 테이블 생성 create TABLE ABCD (id number,name nvarchar2(5)); insert into ABCD values(1,'유재석'); insert into ABCD values(2,'박명수'); insert into ABCD values(3,'하동훈'); --ABCD_DEL_LIST 테이블 생성(ABCD에서 삭제한 데이터를 백업할 테이블) --where 1 = 0을 활용하여 column이름만 가져왔습니다. c.. [ORACLE] SQL - TYPE변수 생성해서 사용하기 안녕하세요. TYPE변수 생성해서 사용하는 예시 공유합니다. 오라클 책에서 실습해보고 복습하기 위해 올려두었습니다. 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 create or replace procedure ABCDE AS Type mytype1 is varray(3) of number(10); Type mytype2 is table of nvarchar2(10); Type mytype3 is table of number(5) index by string(10); my1 mytype1; my2 mytype2; my3 mytype3; idx varchar2(10); begin my1 := mytyp.. 이전 1 다음