oracle sql 存储过程、触发器、方法 存储过程触发器方法
程序员文章站
2022-05-28 13:21:52
...
1、存储过程
create or replace procedure logBigDataTrasfor is queryEndDate Date:=to_date(to_char(sysdate - interval '3' month,'yyyy-MM-dd'),'yyyy-MM-dd'); queryStartDate Date:=to_date(to_char(sysdate - interval '6' month,'yyyy-MM-dd'),'yyyy-MM-dd'); idLog CSSP_USER_ACCESS_LOG_V2.ID_LOG%type; idCsspUser CSSP_USER_ACCESS_LOG_V2.id_cssp_user%type; idCsspOrg CSSP_USER_ACCESS_LOG_V2.Id_Cssp_Org%type; accessDate CSSP_USER_ACCESS_LOG_V2.ACCESS_DATE%type; url CSSP_USER_ACCESS_LOG_V2.Url%type; systemFlag CSSP_USER_ACCESS_LOG_V2.system_flag%type; type cur_type_ref is ref cursor; cur_data cur_type_ref; sqlStr varchar2(2000); totalCount number; totalPages number; i number; v_startRecord number; v_endRecord number; errorCode number; errorMsg varchar2(1000); Begin sqlStr:= 'select count(1) from CSSP_USER_ACCESS_LOG_V2 v2 where v2.access_date<=to_date('''||queryEndDate||''') and v2.access_date>= to_date('''||queryStartDate||''')'; execute immediate sqlStr into totalCount; IF MOD(totalCount,500) = 0 THEN totalPages := totalCount / 500; ELSE totalPages := totalCount / 500 + 1; END IF; i:=1; for i in 1..totalPages LOOP v_startRecord := (i - 1) * 500 + 1; v_endRecord := i * 500; sqlStr:= 'select tempFData.id_log,tempFData.id_cssp_user,tempFData.id_cssp_org,tempFData.access_date,tempFData.url,tempFData.system_flag from (select tempData.*,ROWNUM as rn from (select * from CSSP_USER_ACCESS_LOG_V2 v2 where v2.access_date<=to_date('''||queryEndDate||''') and v2.access_date>= to_date('''||queryStartDate||''') order by access_date desc) tempData where ROWNUM<='||v_endRecord||') tempFData where tempFData.rn >= '||v_startRecord; open cur_data for sqlStr; loop fetch cur_data into idLog,idCsspUser,idCsspOrg,accessDate,url,systemFlag; exit when cur_data%notfound; insert into test_log_bak(id_log,id_cssp_user,id_cssp_org,access_date,url,system_flag) values(idLog,idCsspUser,idCsspOrg,accessDate,url,systemFlag); end loop; close cur_data; commit; end LOOP; EXCEPTION WHEN dup_val_on_index THEN --重复操作 errorCode := SQLCODE; errorMsg := SUBSTR(SQLERRM, 1, 200); dbms_output.put_line(errorCode || ',' || errorMsg); END ;
2、触发器
create or replace trigger test_big_data_user_insert_tr before insert or update on EGISSMTSDATA.test_big_data_user for each row declare date_created Date; date_updated Date; begin date_created:=sysdate; date_updated:=sysdate; case when updating then :new.date_updated := date_updated; when inserting then :new.date_updated := date_updated; :new.date_created := date_created; end case; end;
3、方法
--创建表 create table EGISSMTSDATA.test_uid_code_table ( table_name varchar2(100) NOT NULL, table_id_val number, description varchar2(300) ) tablespace EGISSMTSDATA pctfree 10 initrans 1 maxtrans 255 storage ( initial 64K next 1M minextents 1 maxextents unlimited ); alter table EGISSMTSDATA.test_uid_code_table add constraint PK_TEST_TABLE_NAME primary key (table_name); create public synonym test_uid_code_table for EGISSMTSDATA.test_uid_code_table; --插入数据 insert into test_uid_code_table(table_name,table_id_val,description) values('HELLOWORLD',0,'test') select testNextVal('HELLOWORLD') from dual; --function create or replace function testNextVal( tableName in varchar2 ) return number is currentVal number; sqlStr varchar(2000); PRAGMA AUTONOMOUS_TRANSACTION; begin select table_id_val into currentVal from test_uid_code_table where table_name = tableName; currentVal:=currentVal+1; sqlStr:= 'update test_uid_code_table set table_id_val = '||currentVal ||'where table_name ='''||tableName||''''; EXECUTE immediate sqlStr; commit; return(currentVal); end testNextVal;
上一篇: 这里怎么理解呢
下一篇: proto3语法记录