oracle 학습 노트

/*
     cursor 
*/
declare
  cursor c is   --               
    select * from emp ;
  v_emp c%rowtype;
begin
  open c;  --open               
  loop 
    fetch c into v_emp;
    exit when (c%notfound);
    dbms_output.put_line(v_emp.ename);
  end loop;
	close c;  --    
end;
/


declare
  cursor c is   
    select * from emp ;
  v_emp c%rowtype;
begin
  open c;    
  fetch c into v_emp;
  while(c%found) loop   
    dbms_output.put_line(v_emp.ename);
    fetch c into v_emp ;
  end loop;
	close c;  
end;
/




declare
  cursor c is   
    select * from emp ; 
begin
  for v_emp in c loop
      dbms_output.put_line(v_emp.ename);
  end loop;
end;
/


--     
declare
	cursor c(v_deptno emp.deptno%type,v_job emp.job%type) is
		select ename,sal from emp where deptno=v_deptno and job=v_job;
	--v_temp c%rowtype;
begin
	for v_temp in c(30,'CLERK') loop
		dbms_output.put_line(v_temp.ename);
	end loop;
end;
/
--     
declare
	cursor c is 
		select * from emp2 for update;
begin
	for v_temp in c loop
		if(v_temp.sal<2000) then
			update emp2 set sal=sal*2 where current of c ;
		elsif(v_temp.sal=5000) then
			delete from emp2 where current of c;
		end if;
	end loop;
	commit;
end;
/
/*
       Procedure
           create procedure  
*/

--    
set serveroutput on;
--        
create or replace procedure p_update_emp
as 
	cursor c is 
		select * from emp for update;
begin
	for v_emp in c loop
		if(v_emp.deptno = 10) then
			update emp set sal=sal+10 where current of c;
		elsif(v_emp.deptno = 20) then
			update emp set sal=sal+20 where current of c;
		else
			update emp set sal=sal+50 where current of c;
		end if ;
	end loop;
	commit;
end;
/

--        
/*
        :              ,  p_update  procedure_name
      ,  object_name,procedure_name  
*/
select * from user_procedures;

--       (   )
begin
  p_update_emp;
end;
--       (   )
exec p_update_emp;

--         
/*
	in :      
	out:      
	              
	                   

	  :             ,       
	   v_a number(2)    ,   v_a number
*/
create or replace procedure p_in_out
	(v_a in number,v_b number,v_ret out number,v_temp in out number)
as
	begin
		if(v_a > v_b) then
			v_ret := v_a ;
		else
			v_ret := v_b ;
		end if ;
		v_temp := v_temp +1 ;
	end;
/

--        
declare
	v_a number := 3 ;
	v_b number := 4 ;
	v_ret number;
	v_temp number := 5 ;
begin
	p_in_out(v_a,v_b,v_ret,v_temp);
	dbms_output.put_line(v_ret);
	dbms_output.put_line(v_temp);
end;
/
--     4   6 


--      
drop procedure p_update_emp;  


declare 
  v_a number :=1;
  v_b number :=2;
  v_set number:=3;
  v_temp number :=4;
begin
  p_in_out(v_a,v_b,v_set,v_temp);
  dbms_output.put_line(v_set);
  dbms_output.put_line(v_temp);
end;
/
/*
      trigger
	              
*/

--      emp_log,      emp       
create table emp_log
(
	uname varchar2(20),
	action varchar2(10),
	atime date
);

--     
/*
	    :insert|delete|update        |  |     
	    :after|before     |    
	   :each row           ,            
*/
create or replace trigger trig
	after insert or delete or update on emp for each row
begin
	if inserting then
		insert into emp_log(uname,action,atime) values(USER,'insert',sysdate);
	elsif updating then
		insert into emp_log(uname,action,atime) values(USER,'update',sysdate);
	elsif deleting then
		insert into emp_log(uname,action,atime) values(USER,'delete',sysdate);
	end if ;
end;
/

--  
update emp set sal=sal*2 where deptno=30;
delete from emp where empno = 7369 ;


--     
select * from emp_log;

--? :       ,               ,        ,             
    -- : update dept set deptno=99 where deptno=10

--     
drop trigger trig;

create or replace trigger trig
	after update on dept for each row
begin
	update emp set deptno = :NEW.deptno where deptno = :OLD.deptno ;
end;
/
/*
	    update            :NEW      :OLD     
*/
update dept set deptno=99 where deptno=10;

Oacle 시퀀스 와 동의어
/*
     sequence :
        ,           (         )
     start with number :        
     increment by number :       
     nomaxvalue :      ,      
     nocycle :    
     cache number :           sequence ,        .
     nocache:         down (shutdown abort),cache  sequence    。      create sequence    nocache      
     
       :
  1. insert   values
  2. update   set
  
*/ 

--    
/*    create sequence   create any sequence  */
create sequence seq_up1 start with 1 increment by 1 nomaxvalue nocycle  cache 10;
create sequence seq_up2 start with 10 increment by 2 maxvalue 100 cycle nocache;

--    
select seq_up1.nextval from dual ;  --     ,       
select seq_up1.currval from dual ;  --       

select seq_up2.nextval from dual ;
select seq_up2.currval from dual ;

insert into t10(id,name,address) values(seq_up2.nextval,'  ','  ');

--    
alter sequence seq_up2 increment by 1 maxvalue 1000 ;

--    
drop sequence seq_up2 ;


/*
         synonyms
     
       :          ,                  ;
                    ,                   ;
                            ,        。 
     
*/

--     
conn sys/change_on_install ;  -- sys/change_on_install      
select * from emp ;           --  emp        
select * from scott.emp ;     --    ,  emp    scott 
create synonym emp for scott.emp ;  --     emp,     scott.emp   
select * from emp ;           --        (    )   ,ok

--       
select * from user_synonyms ;

--     
drop synonym emp ;

--       
create public synonym dept for scott.dept;

--       
drop public synonym dept ;

주: 동의어 의 사용 은 보통 Oacle 데이터베이스 에 있 습 니 다. 서로 다른 방안 이 있 습 니 다. 구체 적 인 방안 에서 동의어 로 다른 표 의 데 이 터 를 참조 할 수 있 습 니 다. 마치 자신의 표 와 같 습 니 다.
상용 ORACLE 문장
drop TABLE FBI_BAI_TRANS_HIS; commit; --      
select * from all_users;    --             
select count(*) from dba_tables t where t.owner='TEST'; --            

Oacle 은 사용자 의 모든 시 계 를 삭제 합 니 다. 아래 SQL 은 모든 시 계 를 삭제 하 는 SQL 을 생 성 합 니 다.
select 'drop table '||table_name||';' 
from cat 
where table_type='TABLE' 

좋은 웹페이지 즐겨찾기