Oracle SQL 스 크 립 트 학습 기록 5

5152 단어 sql
declare
x number;
begin
x:=0;
loop
x:=x+1;
if x>=3 then
exit;
end if;
DBMS_OUTPUT.PUT_LINE(‘ :x=’||x);
end loop;
DBMS_OUTPUT.PUT_LINE(‘ :x’||x);
end;
/
—-while
declare
x number;
begin
x:=0;
while x<=3 loop
x:=x+1;
DBMS_OUTPUT.PUT_LINE(‘ :x=’||x);
end loop;
DBMS_OUTPUT.PUT_LINE(‘out:x=’||x);
end ;
/
—-for      declare            
begin
for i in 1..5 loop
DBMS_OUTPUT.PUT_LINE(‘i=’||i);
end loop;
DBMS_OUTPUT.PUT_LINE(‘end of for loop’);
begin
for i in reverse 1..5 loop
DBMS_OUTPUT.PUT_LINE(‘i=’||i);
end loop;
DBMS_OUTPUT.PUT_LINE(‘end of for loop’);
declare
test varchar(12);
begin
select name into test from  deptement where id=’tt’;
DBMS_OUTPUT.PUT_LINE(test);
exception
when NO_DATA_FONUD THEN 
DBMS_OUTPUT.PUT_LINE(‘NOTHINGS’);
END;
      
DECLARE
tname varchar2(10);
e exception;
begin
select name into tname from deptment where id=’01′;
if tname<>’B  ’ then
raise e;//    
end if ;
DBMS_OUTPUT.PUT_LINE(tname);
exception
where e then
DBMS_OUTPUT.PUT_LINE(‘false is not b’);\
end ;
/
—      
declare
type myrecord is RECORD
id varchar2(10),
name varchar2(10);
real_record myrecord;—  
begin
select eid,ename into real_record from emp where eid=’001′;
DBMS_OUTPUT.PUT_LINE(real_record.id||’,'||real_record.name);
end;
/
declare
type myrecord is RECORD
id emp.id%type,
name varchar2(10);
real_record myrecord;—  
begin
select eid,ename into real_record from emp where eid=’001′;
DBMS_OUTPUT.PUT_LINE(real_record.id||’,'||real_record.name);
end;
/
declare
myrec emp%ROWTYPE;
begin
select * into myrec from emp where eid=’001′;
DBMS_OUTPUT.PUT_LINE(myrec.eid||’,'myrec.ename);
end;
/
—  
declare
CURSOR mycur is
select * from books ;
myrecord books%rowtype;
begin
open mycur –    
fetch mycur into myrecord; –          
while mycur%FOUND then loop
DBMS_OUTPUT.PUT_LINE(myrecord.books_id||’ , ‘||myrecord.books_name);
fetch mycur into myrecord;
end loop;
close mycur;
end ;
/
—–       
declare
cursor cur_para(id varchar2) is 
select books_name from books where books_id=id;
t_name books.books_name%type;
begin
open cur_para(’0001′);
loop
fetch cur_para into t_name;
exit when cur_para%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(t_name);
end loop;
close cur_para;
end ;
/
declare
cursor cur_para(id varchar2) is 
select books_name from books where books_id=id;
——for          open   close
begin
DBMS_OUTPUT.PUT_LINE(‘***********RESLUES***********’);
FOR cur in cur_para(’0001′) loop
DBMS_OUTPUT.PUT_LINE(cur.books_name);
end loop;
end ;
/
declare
t_name books.books_name%type;
cursor cur(id varchar2) is 
select books_name from books where books_id=id;
begin
if cur%isopen then
DBMS_OUTPUT.PUT_LINE(‘cur is opened’);
else
open cur(’0003′);
end if;
fetch cur into t_name;
DBMS_OUTPUT.PUT_LINE(‘cur is closed’||t_name);
close cur;
end ;
/
————rowcount  use method
declare
t_name varchar2(10);
cursor mycur is
select name from deptment;
begin
open mycur;
loop;
fetch mycur into t_name;
exit when mycur%notfound  or mycur%notfound is null ;
DBMS_OUTPUT.put_line(‘mycur is rowcount is’||mycur%ROWCOUNT);
end loop;
close mycur;
end ;
/
———–        select          for update
declare
cursor cur is
select name from deptment  for update;
text varchar2(10);
open cur;
fetch cur into text;
while cur%rowcount loop
update deptment set name=name||’t’ where CURRENT OF cur;
fetch cur into text;
end loop;
close cur;
end ;
/
——————————–    
begin
for cur in(select name from deptment) loop
dbms_output.put_line(cur.name);
end loop;
end;
/
—————————-    
create or replase proceduer myproc(id in varchar2)—     
is
name varchar2(10);——–      
begin
select books_name into name from books where books_id=id;
DBMS_OUTPUT.PUT_LINE(name);
end ;
end myproc;
/
show errors;—           
show errors procedure myproc
——————–    
declare
tid varchar2(10);
begin
tid:=’0001′;
myproc(tid)l
end;
/
——————-  
begin 
myproc(’0001′);
end;
/
execute myproc(’0001′); —-                      begin  executre
———–   
create or replace trigger del_deptid
after delete on deptment —      
for each row
begin
delete from emp where id:=old.id;
end del_deptid;
/
–alert
create or replace trigger insert_deptid
after insert on deptment —      
for each row
begin
insert into emp(eid,ename,id) valus(’121′,’wqwe’,:new.id);
end insert_deptid;
/
create or replace trigger update_dept
after update on deptment
for each row
begin
update emp set id=:new.id where id=:oid.id;
end;
/
create or replace trigger books_delete
after delete on books
for each row —-     
begin
if  ld.books_id =’0001′ then
RAISE_APPLICATION_ERROR(-20000,’NOT ALLOW DELELE’);  —     20000—20999
end if;
end; 
/
—————-           DBMS_OUTPUT.PUT_LINE();    rollback     commit  
         
create table mylog(curr_user varchar2(100),curr_date date,act char(1));
create or replace trigger dml_aa
after insert or delete or update
begin
if inserting then
insert into mylog values(user,sysdate,’1′);
elsif deleting then
insert into mylog values(user,sysdate,’d');
else
insert into mylog values(user,sysdate,’u');
end if;
end;
/
CREATE OR REPLACE TRIGGER set_no
before insert on auto
for each row
declare
sn number(5);
begin
select myseq.nextval into sn from dual;
:NEW.a:=sn;
end ;
/

좋은 웹페이지 즐겨찾기