Oracle 相关使用-记录

**1.**临时存储过程执行部分逻辑比较多的业务sql,循环赋值,按字段走不同逻辑等均可采用

DECLARE
--密明变量、游标:
grpcode varchar2(5);
entcode varchar2(5);
actdate Date;
itmlevel number(2);
countNum number(1);
BEGIN
grpcode:='JS';
entcode:='JS';
actdate:= date '2022-2-18';
itmlevel:=4;
while itmlevel > 1 loop
--执行语句
for item in (SELECT * FROM table) loop
DBMS_OUTPUT.put_line(item.字段名 || '---' || itmlevel);
--insert操作
select count(1)into countNum from table t where GL_TYPE='D';
if countNum > 0 then
--update
DBMS OUTPUT.put line('');
else
--insert
end if;
end loop;
itmlevel:=itmlevel-1;
end loop;
END;

**2.**字段重新赋值问题
场景说明:金融类系统,有些账务类字段需要保证字段序号的一致性,但是业务场景又存在中间数据撤销的操作,所以没法max累加处理,然后就有了下面这种业务数据重排的操作

update table t1
set t1.needNo = (select ROW_NO from (select ROW_NUMBER() OVER (order by t2.needNo) as ROW_NO,
											needNo,
											otherCloum
										from table t2
										where 1=1) t3
										where t1.needNo = t3.needNo and 1=1)
where 1=1;
--table 是同一个表,重新取相同数据的记录数通过行号来重排
--原字段比较复杂,固定账套,按日前缀等,拼接原字段的一部分,在按固定位数补0重新得到ID
set t1.needNo = concat(substr(t1.needNo,0,13), 
LPAD('000' + (select ROW_NO from (select ROW_NUMBER() OVER (order by t2.needNo) as ROW_NO,
											needNo,
											otherCloum
										from table t2
										where 1=1) t3
										where t1.needNo = t3.needNo and 1=1) , 7, '0'))
where 1=1;

**3.**Oracle 分组取值问题
例如:取各科分数前三的学生

-- 分组聚合 over PARTITION by
select * from (select sub,
					name,
					score,
					ROW_NUMBER() over (PARTITION by sub order by score desc) as num
					from table_score) tt where tt.num <=3 order by sub;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值