PL/SQL学习笔记-条件控制 (三)

一:if条件控制
先看一段程序:

declare
v_content varchar2(66);
begin
select content into v_content from xland where title='xland';
if length(v_content)>6 then
v_content := substr(v_content,0,6)||'...';
else
v_content := v_content||'(全部数据)';
end if;
dbms_output.put_line(v_content);
end;


这段程序用到了if…then else end if结构
1.其中length()函数可以获取一个字符串的字符长度
另外还有lengthb()函数,可以获取一个字符串的字节长度
2.substr()函数,可以按字符长度获取一个字符串的子字符串,
另外还有substrb()函数,可以按字节长度获取一个字符串的子字符串
3.select… 句是从一个表里获取一条数据,并把数据存储在我们定义的变量中
注意一定要是一条数据,因为我们的变量不是table或者record类型
至于这些类型会在后面的文章中提到

另外还有if…then elseif…then else end if结构
这里就不举例子了
注意elseif是连在一起写的,这很像vb的语法,与C#语法有区别

二:case条件控制
先看例子:

declare
v_content number;
begin
select length(content) into v_content from xland where title='xland';
case
when v_content>6 then
dbms_output.put_line('长度为:'||to_char(v_content));
when v_content<6 then
dbms_output.put_line('长度为:'||to_char(v_content));
else dbms_output.put_line('长度为:'||to_char(v_content));
end case;
end;


其中:to_char()是将一个日期或数字转换成字符串类型的函数
我这里只做演示
实际使用当中case分支控制结构当然不会这样写

再看一个例子:

declare
v_content number;
begin
select length(content) into v_content from xland where title='xland';
case v_content
when 16 then
dbms_output.put_line('长度为:'||to_char(v_content));
else
dbms_output.put_line('长度为:'||to_char(v_content));
end case;
end;


这个就不多解释了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值