oracle10g PLSQL基础

plsql变量命名:由字符开头,可以包含数字、下划线、¥、#等,变量长度1-30,大小写不区分,变量名不能是系统关键字。

if语句写法:

例子1:

declare
x varchar2(10);
begin
x:='this is..';
dbms_output.put_line('x的值为:'||x);
end;

例子2:

declare
a number;
b varchar(20);
begin
a:=2;
if a=1 then
b:='a';
elsif a=2 then
b:='b';
else
b:='c';
end if;
dbms_output.put_line('b的值是:'||b);
end;

 


例子3:

declare
a number;
b varchar(20);
begin
a:=2;
case
when a=1 then b:='a';
when a=2 then b:='b';
when a=3 then b:='c';
else
b:='others';
end case;
dbms_output.put_line('b的值是:'||b);
end;

 


循环:注意不要让过程有死循环,要做计数器。

例子1:

declare
x number;
begin
x:=0;
loop
x:=x+1;
if x>=3 then
exit;
end if;
dbms_output.put_line('内'||x);
end loop;
dbms_output.put_line('外:x='||x);
end;


例子2:loop

declare
x number;
begin
x:=0;
loop
x:=x+1;
exit when x>3;
dbms_output.put_line('内'||x);
end loop;
dbms_output.put_line('外:x='||x);
end;

例子2: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('外:x='||x);
end;


例子3:

begin
for i in 1..5 loop
dbms_output.put_line('i='||i);
end loop;
dbms_output.put_line('end of for loop');
end;

例子4:从大到小循环reverse

begin
for i in reverse 1..5 loop
dbms_output.put_line('i='||i);
end loop;
dbms_output.put_line('end of for loop');
end;

例子5:特殊的一种循环

declare
x number;
begin
x:=0;
<<repeat_loop>>
x:=x+1;
dbms_output.put_line(x);
if x<3 then
goto repeat_loop;
end if;
end;


oracle PLSQL 异常通常有两种,一种是系统的,还有一种是自定义的.

 


定义记录使用方法----
 

复合类型的变量使用例子:
declare
type myrecord is recore(
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 recore(
id emp.eid%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.empno||','||myrec.ename);-->可以打印出更多的变量,这里的名称必须和表的名字是相同的。
end;
/


创建一个只读视图:
create or replace view v_read
as
select eid,ename from emp
with read only  ---->规定了只能读取,不能插入。如果视图加了聚合函数那么默认的为只能读取。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值