oracle--PLSQL
기본 개념
가장 간단 한 pl / sql 프로그램 - Hello World
, serveroutput :
set serveroutput on
declare
--
begin
--
dbms_output.put_line('Hello World');
end;
/
Oacle 에서 제공 하 는 프로그램 패키지 찾기:
index.htm :
Books-->PL/-->
PL/SQL Packages and Types Reference:
PL/SQL User's Guide and Reference:
:
desc+ :
desc dbms_output
1. PL / SQL 이란 무엇 인가
PL/SQL(Procedure Language/SQL)
PL/SQL oracle sql
sql ( 、 ), SQL 。
2. PL / SQL 프로그램 구조
declare
( , , )
begin
(DML )...
exception
end;
변수 와 상수 설명:
:char,varchar2,date,number,boolean,long
、 :
var1 char(15);
married boolean :=true;
psal number(7,2);
, my_name emp ename
my_name emp.ename%type;
emp_rec emp%rowtype;
:
emp_rec.ename:='adams';
: constant:
var1 constant char(15);
예:
:
-- 7839
set serveroutput on
declare
--
--pename varchar2(20);
--psal number;
pename emp.ename%type;
psal emp.sal%type;
begin
--
select ename,sal into pename,psal from emp where empno=7839;
dbms_output.put_line(pename||' '||psal);
end;
:
-- 7839
set serveroutput on
declare
-- :
emp_rec emp%rowtype;
begin
select * into emp_rec from emp where empno=7839;
dbms_output.put_line(emp_rec.ename||' '||emp_rec.sal);
end;
주의:
:
1. :=
2. into,into
if 구문:
1.if then 1;
2;
end if;
2.if then 1;
else 2;
end if;
3.if then ;
elseif then ;
else ;
end if;
예:
--
set serveroutput on
--
--num: ,
accept num prompt ' ';
declare
--
pnum number := #
begin
if pnum = 0 then dbms_output.put_line(' 0');
elsif pnum = 1 then dbms_output.put_line(' 1');
elsif pnum = 2 then dbms_output.put_line(' 2');
else dbms_output.put_line(' ');
end if;
end;
순환 구문:
, ; ,
while total<=25000
loop
...
total:=total+salary;
end loop;
, ; ,
loop
exit[when ];
...
end loop;
..
for i in 1..3
loop
;
end loop;
예:
-- 1~10
set serveroutput on
declare
pnum number := 1;
begin
loop
--
exit when pnum > 10;
dbms_output.put_line(pnum);
--
pnum := pnum + 1;
end loop;
end;
커서
문법:
cursor [( [, ]...)]
is select ;
cursor c1 is select ename from emp;
: open c1;( )( )
:fetch c1 into pename;( )
:close c1;( )
:
pename emp ename :
:pename emp.ename%type;
속성:
1. :
%isopen %rowcount ( )
%found %notfound ( )
2. , 300
SQL> show parameter cursor
NAME TYPE VALUE
------------------------------------ -------------------------------- -----------
cursor_sharing string FORCE
cursor_space_for_time boolean FALSE
open_cursors integer 300
session_cached_cursors integer 20
:
:
SQL> show user
USER is "SCOTT"
SQL> conn sys/[email protected]/orcl as sysdba
Connected.
SQL> show user
USER is "SYS"
alter system set open_cursors=400;
3.( ):cursor_sharing ?--->
: EXACT( ), FORCE, SIMILAR
예:
--
set serveroutput on
declare
--
cursor cemp is select ename,sal from emp;
pename emp.ename%type;
psal emp.sal%type;
begin
--
open cemp;
loop
--
fetch cemp into pename,psal;
--
--exit when ;
exit when cemp%notfound;
dbms_output.put_line(pename||' '||psal);
end loop;
--
close cemp;
end;
-- , 1000 800 400
set serveroutput on
declare
--alter table "SCOTT"."EMP" rename column "JOB" to empjob
cursor cemp is select empno,empjob from emp;
pempno emp.empno%type;
pjob emp.empjob%type;
begin
rollback;
open cemp;
loop
--
fetch cemp into pempno,pjob;
exit when cemp%notfound;
--
if pjob = 'PRESIDENT' then update emp set sal=sal+1000 where empno=pempno;
elsif pjob = 'MANAGER' then update emp set sal=sal+800 where empno=pempno;
else update emp set sal=sal+400 where empno=pempno;
end if;
end loop;
close cemp;
--why? ---> ACID
commit;
dbms_output.put_line(' ');
end;
인자 가 있 는 커서:
매개 변수 가 없 는 커서 와 의 차 이 는 형식 참조 와 열 때 값 을 부여 하 는 것 을 정의 하 는 것 입 니 다.
--
set serveroutput on
declare
cursor cemp(dno number) is select ename from emp where deptno=dno;
pename emp.ename%type;
begin
open cemp(20);
loop
fetch cemp into pename;
exit when cemp%notfound;
dbms_output.put_line(pename);
end loop;
close cemp;
end;
예외
예 외 는 프로그램 설계 언어 가 제공 하 는 기능 으로 프로그램의 건장 성과 용 착 성 을 강화 하 는 데 쓰 인 다.
Oacle 이상 처리:
:
no_data_found( )
too_many_rows(select...into )
zero_divide( )
value_error( )
timeout_on_resource( )
문법:
:
declare
( , , )
begin
(DML )...
exception
end;
:
declare
my_job char(10);
v_sal emp.sal%type;
no_data exception;
cursor c1 is select
distinct job from emp
order by job;
begin
open c1;
fetch c1 into v_job;
if c1%notfound then raise no_data;
end if;
...
exception
when no_data then insert into emp
values('fetch ');
end;
declare :
out_of exception;
:
raise out_of;
exception :
when out_of then...
예:
-- 0
set serveroutput on
declare
pnum number;
begin
pnum := 1/0;
exception
when zero_divide then dbms_output.put_line('1:0 ');
dbms_output.put_line('2:0 ');
when value_error then dbms_output.put_line(' ');
when others then dbms_output.put_line(' ');
end;
:
-- 50
set serveroutput on
declare
cursor cemp is select ename from emp where deptno=50;
pename emp.ename%type;
--
no_emp_found exception;
begin
open cemp;
--
fetch cemp into pename;
if cemp%notfound then
--
raise no_emp_found;
end if;
--pmon: process monitor
close cemp;
exception
when no_emp_found then dbms_output.put_line(' ');
when others then dbms_output.put_line(' ');
end;
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.