plsql程式設計語法

2021-06-08 16:17:37 字數 3206 閱讀 2582

--普通變數

/*declare

u_name varchar2(20):='zhang san';

begin

dbms_output.put_line(u_name);

end;

*/--獲取使用者輸入

/*declare

u_name varchar2(20):='&aaa';

begin

dbms_output.put_line('hello '||u_name);

end;

*/--根據使用者輸入的年齡判斷

/*declare

u_age number :=&age;

msg varchar2(100);

begin

if u_age<18 then

msg:='年齡太小';

elsif u_age <30 then

msg:='您可以註冊';

else

msg:='年齡太大';

end if;

dbms_output.put_line(msg);

end;

*/--判斷範圍

/*declare

u_age number :=&age;

msg varchar2(100);

begin

case

when u_age<18 then msg:='太小';

when u_age<50 then msg:='正好';

else

msg:='太大';

end case;

dbms_output.put_line(msg);

end;

*//*

declare

u_age number :=&age;

msg varchar2(100);

begin

case u_age

when 18 then msg:='太小';

when 50 then msg:='正好';

else

msg:='太大';

end case;

dbms_output.put_line(msg);

end;

*/--根據使用者輸入數字,迴圈輸出

/*declare

u_num number:=&nums;

i number:=0;

begin

loop

dbms_output.put_line(i);

i:=i+1;

if i>u_num then

exit;

end if;

end loop;

end;

*//*

declare

u_num number:=&nums;

i number:=0;

begin

while i

declare

ename varchar2(10);

job varchar2(10);

cursor cur_emp is

select ename,job from emp where empno=7369;

begin

open cur_emp;

fetch cur_emp into ename,job;

if cur_emp%notfound then

dbms_output.put_line('沒有資料');

else

dbms_output.put_line(ename||'*****'||job);

end if;

end;          

*/--loop

declare

ename varchar2(10);

job varchar2(10);

cursor cur_emp is

select ename,job from emp

where sal>2000;

begin

open cur_emp;

loop

fetch cur_emp into ename,job;

if cur_emp%notfound then

exit;

end if;

dbms_output.put_line(ename||'  '||job);

end loop;

close cur_emp;

end;

---while

declare

ename varchar2(10);

job varchar2(10);

cursor cur_emp is

select ename,job from emp

where sal>2000;

begin

open cur_emp;

fetch cur_emp into ename,job;

while cur_emp%fount loop

dbms_output.put_line(ename||'  '||job);

fetch cur_emp into ename,job;

end loop;

close cur_emp;

end;

---for

declare

fname varchar2(20);

lname varchar2(20);

cursor t_student is select firstname, lastname  from students where id<=1005 ;

begin

for t_students in t_student loop

fname:=t_students.firstname;

lname:=t_students.lastname;

dbms_output.put_line('學員姓名:'||fname||''||lname);

end loop;

end;

declare

emp_count number;

begin

select count(empno) into emp_count from emp;

dbms_output.put_line(emp_count);

end;

PL SQL基礎語法

1.分支結構 pl sql中,使用if關鍵字作為分之結構的程式起始段。總體有以下幾種分支結構 1 if condition then statement end if 2 if condition then statement else then statement end if 3 if cond...

PL SQL語法 游標

oracle中的游標分為顯式游標和隱式游標。隱式游標是系統自動為你建立的。顯式游標是使用者通過建立cursor的方式來建立。在oracle中有三種型別的游標 1 不帶引數的游標 eg cursor customer cur is select from customer s 2 帶引數的游標 eg ...

PLSQL語法基礎

變數普通變數宣告方式示例 created on 2019 7 17 by zhou declare v name varchar 20 v sal number v addr varchar 20 begin v name 張三 v sal 10000 select 長沙 into v addr f...