设为首页 收藏本站
查看: 725|回复: 0

[经验分享] oracle学习——PL/SQL

[复制链接]

尚未签到

发表于 2016-7-21 09:37:26 | 显示全部楼层 |阅读模式
--绝对值,取余,判断数值正负函数
select abs(100),abs(-100),abs('100') from dual;
select mod(100,10),mod(100,0) ,mod(34,7) from dual;
select sign(-9),sign(10) from dual;
--四舍五入截取函数
select round(98.36,1) from dual;
select trunc(23562.3,-4) ,trunc(23532.34634,4) from dual;
insert into productinfo(productid,productname,productprice,quantity) values('024004001','鞋子',22.3,10);
set serveroutput on;
--记录类型
declare
type product_rec is record (
v_productid productinfo.productid%type,
v_productname varchar2(20),
v_productprice number(8,2)
);
v_product product_rec;
v_product2 productinfo%rowtype;
begin
select productid,productname,productprice into v_product from productinfo where productid='024004001';
select * into v_product2 from productinfo where productid='024004001';
dbms_output.put_line(v_product2);
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
type t1 is table of productinfo%rowtype index by binary_integer;
type t2 is table of productinfo.productname%type index by varchar(10);
v_t1 t1;
v_t2 t2;
begin
v_t2(0) := '正确';
v_t2(1) := '错误';
v_t2('hello'):= 'hello';
select * into v_t1(1) from productinfo where productid='024004001';
dbms_output.put_line('v_t1(1):'||v_t1(1).productname);
dbms_output.put_line('v_t2(1):'||v_t2(1));
dbms_output.put_line('v_t2(0):'||v_t2(0));
dbms_output.put_line('v_t2(hello):'|| v_t2('hello'));
end;
/
--varray变长数组
declare
type varr is varray(100) of varchar(10) not null;
v_array varr := varr('a','b','c','hello');
begin
v_array(2):='sdfsdf';
dbms_output.put_line(v_array(2));
dbms_output.put_line(v_array(1));
end;
/
declare
v_price number(8,2):=1.4;
begin
if v_price > 12 then dbms_output.put_line('big than 12');
elsif v_price <10 then  dbms_output.put_line('less than 12');
end if;
case v_price
when 2 then dbms_output.put_line('2');
when 1.4 then dbms_output.put_line('1.4');
--when v_price<10 then  dbms_output.put_line('s')
else dbms_output.put_line('no');
end case;
end;
/
--loop while-loop
declare
v_price number(8,2):=1.4;
begin
<<basic_loop>>
loop
v_price := v_price+1;
dbms_output.put_line('basic'||v_price);
/** if v_price > 210 then
exit basic_loop;
end if;**/
exit basic_loop when v_price > 210;
end loop;
/****/
<<while_loop>>
while v_price<410
loop
v_price:=v_price+1;
dbms_output.put_line('while'||v_price);
end loop;
<<for_loop>>
for i in 1..200
loop
dbms_output.put_line('for'||i);
end loop;
end;
/
--以上是基础,下面是PL/SQL使用DML和DDL
declare
v_category categoryinfo.categoryid%type;
v_categoryinfo categoryinfo%rowtype;
begin
select categoryid into v_category from categoryinfo where categoryinfo.categoryid='f';
exception
when no_data_found then
dbms_output.put_line('没有对应的产品类型编号,将添加编号');
insert into categoryinfo(categoryid,categoryname) values('f','食物');
commit;
select * into v_categoryinfo from categoryinfo where categoryinfo.categoryid='f';
dbms_output.put_line(v_categoryinfo.categoryid||','||v_categoryinfo.categoryname);
when too_many_rows then
dbms_output.put_line('不止一条记录被发现');
end;
/
declare
v_ddl varchar(200);
begin
v_ddl:='
create table testtable(
id varchar2(100) primary key not null
)';
execute immediate v_ddl;
end;
/
--查询预定义异常
select * from dba_source where name='STANDARD' and text like '%EXCEPTION_INIT%';
--自定义异常
declare
v_char number(8,0):=9.9;
myException Exception;
pragma exception_init(myException,-11111);
begin
/** **/
if v_char=10 then raise myException;
end if;
exception
when myException then dbms_output.put_line('myException raise');
end;
drop function testfun;
create function testfun
return number
as
v_count number(8,0);
begin
select count(*) into v_count from categoryinfo;
return v_count;
end;
select testfun() from dual;

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-247220-1-1.html 上篇帖子: oracle中创建sequence 下篇帖子: Oracle分页导数据
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表