PLSQL整理

declare
x varchar2(10);		--//定义变量
begin
x:='hello';		--//给变量赋值
DBMS_OUTPUT.PUT_LINE(x);--//输出变量
end;




declare
x number(10);
begin
x:=2222;
dbms_output.put(x);	--//输出变量
dbms_output.new_line;	--//输出变量
end;







declare
x number(3):=100;	--//定义变量的时候初始化
begin
dbms_output.put_line(x);
end;
/



declare
a int;
b int;
begin
a:=19;
b:=10;
dbms_output.put_line(a||b);	--输出1910
end;
/



declare
x int:=1;		--定义变量并初始化
y int:=2;		--定义变量并初始化
begin
dbms_output.put_line(x+y);	--输出3
end;
/



declare 
a int :=23;
b int :=10;
begin
a:=a+b;
dbms_output.put_line(a);	--输出33
end;
/



--分支语句
declare
a number;
b varchar2(10);
begin
a:=4;
if a=1 then
b:='a';
elsif a=2 then
b:='b';
else
b:='c';
end if;
dbms_output.put_line(b);	--输出结果为:c
end;
/


declare
a number:=3;
b number;
begin
if a<3 then b:=1;
elsif a>3 then b:=5;
else b:=3;
end if;
dbms_output.put_line(b);
end;
/



declare
a number;
b varchar2(10);
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);
end;
/


--循环语句

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


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



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;
/



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




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



--goto

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;
/



--错误的plsql
/*
declare
test varchar2(10);
begin
select ename into test from emp where empno=55;
dbms_output.put_line(test);
end;
/
*/

--这是正确的plsql
declare
test varchar2(10);
begin
select ename into test from emp where empno=55;
dbms_output.put_line(test);

exception 
when No_DATA_FOUND then
dbms_output.put_line('没有找到数据');
end;
/





declare
ename varchar2(10);
begin
select ename into ename from (select ename,rownum r from emp order by sal d
esc) where r <2;
dbms_output.put_line(ename);
end;
/


 declare
 test varchar2(10);
 begin
 select ename into test from emp ;
 dbms_output.put_line(test);
 exception
 when no_data_found then
 dbms_output.put_line('没有找到数据!');
 when too_many_rows then
 dbms_output.put_line('返回的数据行太多!');
 when others then
 dbms_output.put_line('其他问题!');
 end;
 /


 declare
 a number(4);
 begin
 a:=a/0;
 exception
 when no_data_found then
 dbms_output.put_line('没有找到数据!');
 when too_many_rows then
 dbms_output.put_line('返回的数据行太多!');
 when others then
 dbms_output.put_line('其他问题!');
 end;
 /

declare
ename varchar2(10);
begin
select ename into ename from emp where empno=33;
exception
when others then
dbms_output.put_line('出错了!');
end;
/



--记录
declare
type myperson is person(
userid varchar2(10),
name varchar2(10),
pwd varchar2(10));
jilu myperson;
begin
select userid,name,pwd into real_record from person where person='cs' ;
dbms_output.put_line(jilu.name);
end;
/


转载于:https://my.oschina.net/csmw00/blog/674010

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值