首页 技术 正文
技术 2022年11月15日
0 收藏 378 点赞 2,915 浏览 3282 个字

写在前面

这段时间工作最长接触到的就是Oracle数据库,不论查数据,还是统计、运行job,都离不开PL/SQL 存储过程,下面就整理下经常用到的知识。

一、Function函数

函数是执行非查询语句的方法。

  • 创建返回list的function
CREATE OR REPLACE FUNCTION FNTOPWEEKESLIST (
ininttype IN INTEGER,
outcurlist OUT pkgrefcursor.refcursor
)
RETURN INTEGER
IS
Begin
OPEN outcurlist FOR
SELECT t.all, a.bookname
FROM tbbookestimatestats t, tbbook a
WHERE t.intype = ininttype
and t.bookid = a.bookid
and a.freetype = 0;
RETURN 0;
END;
  • 创建带分页的返回list的function
CREATE OR REPLACE FUNCTION fn_source_pay_getlist(indtbegin       DATE,
indtend DATE,
invarsourcename VARCHAR2,
inintpagesize IN INTEGER,
inintpageindex IN INTEGER,
outintRowCount out integer,
outcurlist OUT pkg_refcursor.ref_cursor)
RETURN INTEGER
as
begin
select count(1)
into outintRowCount
from tbcl_resu_payuser
where source = case
when length(invarsourcename) > 0 then
invarsourcename
else
source
end
and trunc(datetime) >= indtbegin
and trunc(datetime) < indtend;
open outcurlist for
select *
from (select a.*, row_number() over(order by a.datetime) rn
from (select source,
datetime,
payusercnt1,
payusercnt2,
payusercnt3,
payusercnt4
from tbcl_resu_payuser
where source = case
when length(invarsourcename) > 0 then
invarsourcename
else
source
end
and trunc(datetime) >= indtbegin
and trunc(datetime) < indtend
order by datetime) a)
where rn between inintpagesize * (inintpageindex - 1) + 1 and
inintpagesize * inintpageindex;
return 0;
end;
  • 返回普通值的function
CREATE OR REPLACE FUNCTION FNACTIVEDAYSUSECNTGET (inintactivetype INTEGER,
inintuserid integer,
outintgetcnt out integer,--总抽奖次数
outintusercnt out integer)--用户抽奖次数
RETURN INTEGER
IS
获取活动当天领取次数
vcnt INTEGER;
BEGIN
SELECT NVL (MAX (cnt), 0)
INTO outintgetcnt
FROM tbactivestats
WHERE logtime = TRUNC (SYSDATE) AND activetype = inintactivetype;
SELECT count(1)
INTO outintusercnt
FROM tbuseractivegiftlog
WHERE userid = inintuserid AND activetype = inintactivetype;
RETURN 0;
END;

或者这样:

CREATE OR REPLACE FUNCTION FNUSERCOSTGET(invarptid   IN varchar2,
inintbookid in integer)
RETURN integer
IS
vamount integer;
BEGIN
select nvl(max(amount), 0)
into vamount
from tbuserbookcost
where ptid = lower(invarptid)
and bookid = inintbookid;
return(vamount);
EXCEPTION
WHEN others THEN
return(0);
END;

二、Procedure过程

  • 更新update
CREATE OR REPLACE PROCEDURE "ADDUPDATEBOOKLOG" (inintbookid   IN  INTEGER,
inintfreetype IN INTEGER,
outintresult OUT INTEGER) AS
/*
功能:插入一条记录
传入参数:bookid,freetype
输出参数:是否插入成功
*/
BEGIN
如果存在就更新记录
UPDATE tbupdatebooklog
SET createtime = SYSDATE, freetype = inintfreetype
WHERE bookid = inintbookid;
--如果不存在就插入记录
IF SQL%ROWCOUNT = 0 THEN
INSERT INTO tbupdatebooklog(bookid, createtime, freetype)
VALUES(inintbookid, SYSDATE, inintfreetype);
END IF;
COMMIT;
outintresult := 0;
EXCEPTION
WHEN OTHERS THEN
outintresult := 1;
ROLLBACK;
END AddUPdateBookLog;

或者:

CREATE OR REPLACE PROCEDURE PRBOOKSIGNSTATUSSET(inintBookId     integer,
invarSignStatus varchar2,
outintresult out integer) isbegin
update tbbook t
set t\.signstatus = invarSignStatus
where bookId = inintBookId;
commit;
outintresult := 0;
end prbookSignStatusset;
  • 插入insert
CREATE OR REPLACE PROCEDURE "PRBOOKTYPEUPDLOGINS" (
inintbookid IN INTEGER,
ininttypeid IN INTEGER,
invatypename IN varchar2,
outintresult OUT INTEGER
)
IS
vcnt INTEGER;
BEGIN
insert into tbbooktypeupdlog(bookid,updatetime,typeid,typename)
values (inintbookid,sysdate,ininttypeid,invartypename);
commit;
outintresult:=0;
END;

三、Package包

包是有包头和包体组成的一组具有共性的函数或者过程。

  1. 声明包
CREATE OR REPLACE PACKAGE PKGTEST is
TYPE refcursor IS REF CURSOR;
procedure add();
procedure update();
procedure get();
end pkgTEST;
  1. 定义包体
CREATE OR REPLACE PACKAGE BODY PKGTEST is
TYPE refcursor IS REF CURSOR;
procedure add() IS
BEGIN
...
END;
procedure update() IS
BEGIN
...
END;
procedure get() IS
BEGIN
...
END;
end pkgTEST;
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,489
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,290