Create Table partitions in oracle stored procedures

Source: Internet
Author: User
Oracle Stored Procedure create table partition Oracle Stored Procedure partition (50); -- table name v_Mms_Content_TabVARCHAR2 (50); v_Mms_User_TabVARCHAR2 (50); v_TableSpaceVARCHAR2 (20); -- tablespace v_PartPreFl

Create or replace procedure partition AS v_Mms_Task_Tab VARCHAR2 (50); -- table name v_Mms_Content_Tab VARCHAR2 (50); v_Mms_User_Tab VARCHAR2 (50 ); v_TableSpace VARCHAR2 (20); -- tablespace v_PartPreFl

Create Table partitions in oracle stored procedures
Oracle Stored Procedure
Create or replace procedure tables AS v_Mms_Task_Tab VARCHAR2 (50); -- table name v_Mms_Content_Tab VARCHAR2 (50); v_Mms_User_Tab VARCHAR2 (50); v_TableSpace VARCHAR2 (20 ); -- tablespace v_PartPreFlag VARCHAR2 (50); -- partition name identification v_SqlCursor NUMBER; -- cursor v_SqlExec VARCHAR2 (2000); -- execution statement v_PartPreDate VARCHAR2 (20); -- partition date v_RangeValue NUMBER; v_RangeDate NUMBER; v_Rows NUMBER (30): = 0; v_Num NUMBER (30): = 0; vErrInfo VARCHAR2 (200); p_DateFrom NUMBER; p_PartNum NUMBER; p_Range NUMBER; v_in v_Mms_Task_Tab: = 'bip _ number'; v_Mms_Content_Tab: = 'bip _ MMS_MT_CONTENT_TAB_TEST '; v_Mms_User_Tab: = 'bip _ test'; -- read the configuration parameter begin select TO_NUMBER (VALUE) INTO p_DateFrom FROM BIP_OTHERS_PROPERTIES_TAB where name = 'P _ DateFrom '; SELECT TO_NUMBER (VALUE) INTO p_PartNum FROM BIP_OTHERS_PROPERTIES_TAB where name = 'P _ PartNum'; SELECT TO_NUMBER (VALUE) INTO p_Range FROM BIP_OTHERS_PROPERTIES_TAB where name = 'P _ range'; exception when others then begin p_DateFrom: = 0; p_PartNum: = 1; p_Range: = 180; END; -- insert into BIP_LOG_STAT_EXEC_TAB VALUES (TO_CHAR (SYSDATE, 'yyyymmddhh24miss '), 'bip _ mms_partition_proc_add', 'begin'); COMMIT; -- add partition for I IN 1 .. p_PartNum LOOP -- add partition v_PartPreDate: = TO_CHAR (SYSDATE + p_DateFrom + I, 'yyyymmdd'); dbms_output.put_line (v_PartPreDate); v_Num: = 0; v_TableSpace: = 'bip _ MMS_TS_TEST '; v_PartPreFlag: = 'mms _ MT_CONTENT'; select count (*) INTO v_Num FROM user_tab_partitions WHERE table_name = v_Mms_Content_Tab and substr (partition_name, 16, 8) = v_PartPreDate; IF v_Num <1 THEN v_RangeDate: = TO_CHAR (SYSDATE + p_DateFrom + I, 'yyyymmdd'); v_RangeValue: = v_RangeDate | '123'; THEN (v_RangeValue ); v_SqlExec: = 'alter table' | v_Mms_Content_Tab | 'add partition' | v_PartPreFlag | '_' | v_PartPreDate | 'values less than (''' | v_RangeValue | ''') TABLESPACE '| v_TableSpace; lower (v_SqlExec); v_SqlCursor: = lower; lower (v_SqlCursor, v_SqlExec, lower); v_Rows: = DBMS_ SQL .EXECUTE (v_SqlCursor); lower (v_SqlCursor ); end if; -- add partition v_PartPreDate: = TO_CHAR (SYSDATE + p_DateFrom + I, 'yyyymmdd'); v_Num: = 0; v_TableSpace: = 'bip _ MMS_TS_TEST '; v_PartPreFlag: = 'mms _ MT_TASK_LOG '; select count (*) INTO v_Num FROM user_tab_partitions WHERE table_name = v_Mms_Task_Tab and substr (partition_name, 17, 8) = v_PartPreDate; IF v_Num <1 THEN v_RangeDate: = TO_CHAR (SYSDATE + p_DateFrom + I, 'yyyymmdd'); v_RangeValue: = v_RangeDate | '123'; v_SqlExec: = 'alter table' | v_Mms_Task_Tab | 'add partition' | v_PartPreFlag | '_' | v_PartPreDate | 'values less than (''' | v_RangeValue | ''') TABLESPACE '| v_TableSpace; lower (v_SqlExec); v_SqlCursor: = lower; lower (v_SqlCursor, v_SqlExec, lower); v_Rows: = DBMS_ SQL .EXECUTE (v_SqlCursor); lower (v_SqlCursor ); end if; -- add partition v_PartPreDate: = TO_CHAR (SYSDATE + p_DateFrom + I, 'yyyymmdd'); v_Num: = 0; v_TableSpace: = 'bip _ MMS_TS_TEST '; v_PartPreFlag: = 'mms _ MT_USER_LOG '; select count (*) INTO v_Num FROM user_tab_partitions WHERE table_name = v_Mms_User_Tab and substr (partition_name, 17, 8) = v_PartPreDate; IF v_Num <1 THEN v_RangeDate: = TO_CHAR (SYSDATE + p_DateFrom + I, 'yyyymmdd'); v_RangeValue: = v_RangeDate | '123'; v_SqlExec: = 'alter table' | v_Mms_User_Tab | 'add partition' | v_PartPreFlag | '_' | v_PartPreDate | 'values less than (''' | v_RangeValue | ''') TABLESPACE '| v_TableSpace; lower (v_SqlExec); v_SqlCursor: = lower; lower (v_SqlCursor, v_SqlExec, lower); v_Rows: = DBMS_ SQL .EXECUTE (v_SqlCursor); lower (v_SqlCursor ); end if; end loop; COMMIT; insert into BIP_LOG_STAT_EXEC_TAB VALUES (TO_CHAR (SYSDATE, 'yyyymmddhh24miss '), 'bip _ mms_partition_proc_add', 'end'); COMMIT; -- delete partition insert into BIP_LOG_STAT_EXEC_TAB VALUES (TO_CHAR (SYSDATE, 'yyyymmddhh24miss '), 'bip _ login', 'begin'); COMMIT; BEGIN v_PartPreFlag: = 'mms _ MT_CONTENT '|' _ '| TO_CHAR (SYSDATE-p_Range, 'yyyymmdd'); dbms_output.put_line (v_PartPreFlag); v_SqlExec: = 'alter table' | PARTITION | 'truncate partition' | v_PartPreFlag; reverse (v_SqlExec); v_SqlCursor: = reverse; reverse (v_SqlCursor, v_SqlExec, DBMS_ SQL .NATIVE); v_Rows: = DBMS_ SQL .EXECUTE (v_SqlCursor); DBMS_ SQL .CLOSE_CURSOR (v_SqlCursor); dbms_output.put_line (v_PartPreFlag | 'truncated'); v_SqlExec: = 'alter table' | Lower | 'drop partition' | v_PartPreFlag; lower (v_SqlExec); v_SqlCursor: = lower; lower (v_SqlCursor, v_SqlExec, DBMS_ SQL .NATIVE); v_Rows: = DBMS_ SQL .EXECUTE (v_SqlCursor); DBMS_ SQL .CLOSE_CURSOR (v_SqlCursor); dbms_output.put_line (v_PartPreFlag | 'dropped'); END; BEGIN v_PartPreFlag: = 'mms _ MT_TASK_LOG '|' _ '| TO_CHAR (SYSDATE-p_Range, 'yyyymmdd'); dbms_output.put_line (v_PartPreFlag); v_SqlExec: = 'alter table' | PARTITION | 'truncate partition' | v_PartPreFlag; reverse (v_SqlExec); v_SqlCursor: = reverse; reverse (v_SqlCursor, v_SqlExec, DBMS_ SQL .NATIVE); v_Rows: = DBMS_ SQL .EXECUTE (v_SqlCursor); DBMS_ SQL .CLOSE_CURSOR (v_SqlCursor); dbms_output.put_line (v_PartPreFlag | 'truncated'); v_SqlExec: = 'alter table' | Lower | 'drop partition' | v_PartPreFlag; v_SqlCursor: = lower; lower (v_SqlCursor, v_SqlExec, lower); v_Rows: = DBMS_ SQL .EXECUTE (v_SqlCursor ); values (v_SqlCursor); dbms_output.put_line (v_PartPreFlag | 'dropped'); END; BEGIN v_PartPreFlag: = 'mms _ MT_USER_LOG '|' _ '| TO_CHAR (SYSDATE-p_Range, 'yyyymmdd'); reverse (v_PartPreFlag); v_SqlExec: = 'alter table' | reverse | 'truncate partition' | v_PartPreFlag; reverse (v_SqlExec); v_SqlCursor: = reverse; values (v_SqlCursor, v_SqlExec, DBMS_ SQL .NATIVE); v_Rows: = DBMS_ SQL .EXECUTE (v_SqlCursor); values (v_PartPreFlag | 'truncated'); v_SqlExec: = 'alter table' | Lower | 'drop partition' | v_PartPreFlag; v_SqlCursor: = lower; lower (v_SqlCursor, v_SqlExec, lower); v_Rows: = DBMS_ SQL .EXECUTE (v_SqlCursor ); VALUES (v_SqlCursor); VALUES (v_PartPreFlag | 'dropped'); END; COMMIT; insert into BIP_LOG_STAT_EXEC_TAB VALUES (TO_CHAR (SYSDATE, 'yyyymmddhh24miss '), 'bip _ failed ', 'end'); COMMIT; exception when others then begin rollback; dbms_output.put_line (TO_CHAR (SQLCODE); vErrInfo: = SUBSTR (SQLERRM, 1,200); limit (TO_CHAR (vErrInfo )); insert into BIP_LOG_STAT_EXEC_TAB VALUES (TO_CHAR (SYSDATE, 'yyyymmddhh24miss '), 'bip _ MMS_PARTITION_PROC_ERROR', vErrInfo); COMMIT; END; end iterator;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.