PL/SQL 语句


--1、pl/sql语句块格式  
--[declare  
--变量名 类型;  
--.....]  
--begin  
-- 语句块  
-- [exception  
--   异常处理]  
--end;  
  
--例如:  
begin  
dbms_output.put_line('hello');  
end;  
  
declare  
num number default 100;  
begin  
dbms_output.put_line(num);  
end;  
  
--2、声明全局变量  
--declare  
--变量名1 类型1 [default 初始值];  
--变量名2 类型2 [:=初始值];  
--.....  
--1)赋初值两种方式default或:=  
--2)赋值方式::=或select ...  into ...  
--3)动态输值  :=&提示 或 :='&提示'  

declare  
text varchar2(50);  
num number;  
begin  
--text:='这是一个文本';  
text:='&输入一个字符串';  
dbms_output.put_line(text);  
select count(empno) into num from emp;  
dbms_output.put_line(num);  
end;  
  
--3、注释  
----:单行注释  
--/*  */:多行注释  
  
--4、类型  
--%type:字段类型  
  
--显示编号为7369的员工姓名  
declare  
xm emp.ename%type;  
begin  
select ename into xm from emp  
where empno=7369;  
dbms_output.put_line(xm);  
end;  
  
--%rowtype:表的行类型  
  
--取出7369这名员工的所有信息  
declare  
jr emp%rowtype;  
begin  
select * into jr from emp  
where empno=7369;  
dbms_output.put_line(jr.ename);  
dbms_output.put_line(jr.hiredate);  
dbms_output.put_line(jr.sal);  
dbms_output.put_line(jr.deptno);  
end;  
  
--5、选择语句  
--1)单分支if语句  
--if  条件  then  
--begin  
--  语句;  
--end;  
--end if;  
  
--求一个数的绝对值  
declare  
n number;  
begin  
   n:=&输入一个整数;  
   if n<0 then  
    n:=-n;  
   end if;  
dbms_output.put_line('绝对值为:'||n);  
end;  
  
--2)双分支if  
--if 条件 then   
--begin   
--  语句块1;  
--end;  
--else  
--begin   
--  语句块2;  
--end;  
--end if;  

--判断一个年份是闰年还是平年  
declare  
y number;  
begin  
y:=&输入一个年份;  
if (mod(y,400)=0) or (mod(y,4)=0) and  (mod(y,100)!=0) then  
 dbms_output.put_line('闰年');  
else   
dbms_output.put_line('平年');  
end if;  
end;  
  
--3)多分支if  
--if 条件1 then   
--begin   
--  语句块1;  
--end;  
--else  if 条件2 then  
--begin   
--  语句块2;  
--end;  
--else  
--begin   
--  语句块3;  
--end;  
--end if;  
--end if;  
  
--判断一个成绩优良中差  
declare  
score number;  
begin  
score:=&输入一个成绩;  
if score>=90 then  
dbms_output.put_line('优秀');  
else if score>=80 then  
dbms_output.put_line('良好');  
else if score>=60 then  
dbms_output.put_line('中等');  
else  
dbms_output.put_line('不及格');  
end if;  
end if;  
end if;  
end;  
  
--6、循环语句  
--1)无条件的循环语句  
--loop   
-- 循环体  
--end loop;
  
--求n的阶乘  
declare  
n number ;  
s number default 1;  
begin  
n:=&输入一个正整数;  
loop  
s:= s*n;  
n:=n-1;  
/*if n<=1 then  
exit;  
end if;*/  
exit when n<=1;  
end loop;  
dbms_output.put_line(s);  
end;  
  
--2)while循环  
--while 条件  
--loop  
--循环体  
--end loop;  
  
declare  
n number ;  
s number default 1;  
begin  
n:=&输入一个正整数;  
while n>1  
loop  
s:= s*n;  
n:=n-1;  
end loop;  
dbms_output.put_line(s);  
end;  
  
--3)for in 循环  
--for 变量  in 起点..终点  
--loop  
--循环体  
--end loop;  
  
declare  
n number ;  
s number default 1;  
i number;  
begin  
n:=&输入一个正整数;  
for i in REVERSE 1..n  
loop  
s:= s*i;  
end loop;  
dbms_output.put_line(s);  
end;  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值