Oracle,實現一條記錄的上移下移
阿新 • • 發佈:2019-01-29
對於一個元組的上移下移排序
1.今天要和大家分享一個Oracle資料庫實現對一條記錄的上移下移功能
對於熟悉PLSQL程式設計的人來說,語法之類自然已經是家常便飯了,在此我就不再贅述,那麼我們直接上程式碼,
表結構程式碼:
create table STORAGE_LOCATION
(
id NUMBER not null,
storage_code VARCHAR2(100) not null,
area_code VARCHAR2(100) not null,
code VARCHAR2(100) not null,
name VARCHAR2(200 ) not null,
shelf_code VARCHAR2(100),
state VARCHAR2(100),
flag VARCHAR2(100),
sort NUMBER,
create_time DATE,
create_user VARCHAR2(100),
update_time DATE,
udapte_user VARCHAR2(100)
)
實現功能程式碼:
create or replace procedure pro_update_location_sort
(
StorageCode varchar2, --倉庫程式碼
sort_no number , --sort_no 當前行sort值
type_char number --type_char 排序型別 1.置頂 2.上移 3.下移 4.置底
) is
--實現功能:倉庫管理中下架路徑的排序
other_sort number;
current_id number;
other_id number;
isRun number:=0;
startTime varchar2(30);
begin
--判斷儲存過程的執行狀態
select count(*) into isRun from oversea_run_status s where s.ems_type='pro_update_location_sort';
if isRun=0 then
insert into oversea_run_status(ems_type,create_on,status)
values('pro_update_location_sort',sysdate,'1');
--插入執行日誌記錄
startTime:=to_char(sysdate,'yyyyMMddHH24miss');
insert into OVERSEA_RUN_LOG(oper_name,oper_type,begin_date,end_date,username)
values('管理下架路徑','pro_update_location_sort',startTime,'','');
commit;
--業務邏輯處理
--置頂
if type_char=1 then
select (nvl(min(sl.sort),0)-1) into other_sort from storage_location sl ;
update storage_location set sort=other_sort where sort=sort_no and storage_code=StorageCode;
commit;
end if;
--上移
if type_char=2 then
select t.id into current_id from storage_location t where t.sort=sort_no and storage_code=StorageCode ;
select t.id, nvl(t.sort,0) into other_id,other_sort from storage_location t
where t.sort=
(select p from
(select sl.storage_code,sl.area_code, sl.sort,lag(sl.sort,1,0) over (order by sl.sort) as p
from storage_location sl where sl.storage_code=StorageCode order by sl.sort asc) c
where c.sort=sort_no
) ;
if other_sort!=0 then
update storage_location set sort=other_sort where id=current_id;
update storage_location set sort=sort_no where id=other_id;
commit;
end if;
end if;
--下移
if type_char=3 then
select t.id into current_id from storage_location t where t.sort=sort_no and storage_code=StorageCode ;
select t.id, nvl(t.sort,0) into other_id,other_sort from storage_location t
where t.sort=
(select p from
(select sl.storage_code,sl.area_code,sl.sort,lead(sl.sort,1,0) over (order by sl.sort) as p
from storage_location sl where sl.storage_code=StorageCode order by sl.sort asc) c
where c.sort=sort_no
) ;
if other_sort!=0 then
update storage_location set sort=other_sort where id=current_id;
update storage_location set sort=sort_no where id=other_id;
commit;
end if;
end if;
--置底
if type_char=4 then
select (nvl(max(sl.sort),0)+1) into other_sort from storage_location sl ;
update storage_location set sort=other_sort where sort=sort_no and storage_code=StorageCode ;
commit;
end if;
--更新執行狀態、執行日誌記錄
delete from oversea_run_status where ems_type='pro_update_location_sort';
update OVERSEA_RUN_LOG l set l.RUN_RESULT='成功',l.end_date =to_char(sysdate,'yyyyMMddHH24miss')
where l.begin_date=startTime;
commit;
end if;
--異常處理
exception when others then
rollback;
--更新執行狀態、執行日誌記錄
delete from oversea_run_status where ems_type='pro_update_location_sort';
update OVERSEA_RUN_LOG l set l.end_date =to_char(sysdate,'yyyyMMddHH24miss'),l.RUN_RESULT='失敗'
where l.begin_date=startTime;
commit;
end pro_update_location_sort;
呼叫,這個我們就不再講了,
我們直接看效果吧:
當然,本人資料庫水平有限,如果資料量大的話,可能撐不起來,歡迎批評扔磚。