Some systems require monthly points tables to store data. The following stored procedure demonstrates how to use the base table to establish a lunar month.
The idea is to deal with:
1: First, establish a good table and the corresponding index for the base table.
2: Save the base table to a table stored procedure in need thereof.
3: the stored procedure to read the configuration table, according to the configuration of the table name, to build a database query table statement corresponding table, and then to replace the table name statement with the table name of the lunar surface, and then they build the table. Go lookup table base table is not indexed, if any, from the database to give statements to build the table index, and then replace the index and table names construction of the table statement, the statement is executed last indexed.
-- oracle环境的sql -- 基础表准备 drop table base_config_monthly_table; create table base_config_monthly_table ( base_table_name varchar2(50), valid_flag number(1) default 1, remark varchar2(1000) ); select * from base_config_monthly_table ; insert into base_config_monthly_table(base_table_name,valid_flag,remark) select 'aa_wanglc_test', 1 , ' WANG provide sample path length ' from Dual; the commit ; - test stand functions the SELECT the REPLACE ( ' 15,800,003,367 ' , substr ( ' 1,580,003,367 ' , . 4 , . 4 ), ' **** ' ) from Dual; SELECT InStr ( ' abcdef ' , ' D ' ) from Dual; SELECT length ( ' ABDF ' ) fromDual; SELECT TO_CHAR (ADD_MONTHS (the trunc (SYSDATE), - . 1 ), ' YYYY ' ) from Dual; SELECT TO_CHAR ( . 1 , ' fm00 ' ) from Dual; the CREATE TABLE aa_wanglc_test the AS the SELECT * the FROM Dual; the SELECT * the FROM aa_wanglc_test; the CREATE the iNDEX idx_wt_d009091 the ON aa_wanglc_test ( dummy ); - establishing monthly base table by table, and the table established by the monthly base table index index the CREATE OR The REPLACE Procedure proc_create_monthly_tables the AUTHID CURRENT_USER the IS v_base_monthly_table base_config_monthly_table % ROWTYPE; - configuration table v_base_ddl VARCHAR2 ( 4000 ); - Save the base table SQL v_sql VARCHAR2 ( 4000 ); - eventually need to use SQL v_new_tableName VARCHAR2 ( 50 ); - months table table name v_yyyy VARCHAR2 ( 10 ); - in variable lunar surface v_month Number the : = 0 ; - lunar month variable v_current_month VARCHAR2 ( 10 ); - the month of the loop variable v_exists_flag Number The : = 0 ; v_base_table_name_upper VARCHAR2 ( 50 ); - a capital base table name format of the Cursor v_cur_get_basetable IS the SELECT * from base_config_monthly_table the WHERE valid_flag = . 1 ; type cur_get_index IS REF Cursor ; -Declare a dynamic cursor type, because the type of cursor is not so to declare a dynamic cursor type, you need to query the index for each base table v_cur_get_index cur_get_index; - declare a dynamic cursor variable v_index_ddl VARCHAR2 ( 4000 ); - indexing SQL v_index_name_str VARCHAR2 ( 30 ); v_index_name_num NUMBER ; v_index_name VARCHAR2 ( 30 ); v_old_index_name VARCHAR2 ( 30 ); the begin Open v_cur_get_basetable; Loop FETCH v_cur_get_basetable INTO v_base_monthly_table; Exit when v_cur_get_basetable%notfound; select to_char(sysdate,'yyyy') into v_yyyy from dual; select upper(v_base_monthly_table.base_table_name) into v_base_table_name_upper from dual; --dbms_output.put_line(v_base_table_name_upper); v_exists_flag := 0; select count(*) into v_exists_flag from user_tables where table_name =v_base_table_name_upper; - if the group table does not exist, this configuration records not processed IF v_exists_flag = 0 the then Continue ; End IF ; - obtaining the base table in the database system built table statement the SELECT the DBMS_METADATA.GET_DDL ( ' TABLE ' , v_base_table_name_upper) INTO v_base_ddl from Dual; - DBMS_OUTPUT.PUT_LINE (v_base_ddl); v_month: = 0 ; Loop v_month: = v_month + . 1 ; Exit when v_month > 12; select to_char(v_month,'fm00') into v_current_month from dual; v_new_tableName := v_base_table_name_upper||'_'||v_yyyy||v_current_month; -- dbms_output.put_line(v_new_tableName); v_exists_flag := 0; select count(*) into v_exists_flag from USER_TABLES WHERE table_name = Upper (v_new_tableName) or table_name = ' " ' || v_new_tableName || ' " ' ; - DBMS_OUTPUT.PUT_LINE (v_exists_flag); - if the monthly table has been created, it is no longer created IF v_exists_flag > 0 the then Continue ; End IF ; - with the table name to replace the lunar base construction of the table statement exemplar name of the SELECT rEPLACE (v_base_ddl, v_base_table_name_upper, v_new_tableName) into v_sql from dual; --dbms_output.put_line(v_sql); execute IMMEDIATE v_sql; -- 查找索引 v_sql := 'SELECT DBMS_METADATA.GET_DDL('''||CHR(73)||'NDEX'', index_name),index_name FROM USER_INDEXES WHERE table_name = '''||v_base_table_name_upper||''' AND UNIQUEness = ''NONUNIQUE'''; open v_cur_get_index for v_sql; - open the cursor, and the results stored in the SQL cursor LOOP FETCH v_cur_get_index INTO v_index_ddl, v_old_index_name; Exit the when v_cur_get_index % NOTFOUND; - exit the loop - a name for the index, whose name is randomly get the SELECT dbms_random.string ( ' X ' , . 8 ) the INTO v_index_name_str the FROM Dual; SELECT the trunc (DBMS_RANDOM.VALUE ( 0 , 100000 )) the INTO v_index_name_numfrom Dual; v_index_name: = ' idx_ ' || v_index_name_str || ' _ ' || the TO_CHAR (v_index_name_num); - the index name in the statement indexing get rid of the SELECT REPLACE (v_index_ddl, v_old_index_name, v_index_name) INTO v_sql from Dual ; - dbms_output.put_line (v_sql); - the statement indexing table names get rid of the SELECT REPLACE (v_sql, v_base_table_name_upper, v_new_tableName) INTO v_sql from Dual; - dbms_output.put_line (v_sql); the Execute IMMEDIATE v_sql; END LOOP; - creating an index of dynamic cursors end End Loop; End Loop; use Close v_cur_get_basetable; End ; - view the stored procedure is not a mistake select * from USER_ERRORS; - execute a stored procedure the begin proc_create_monthly_tables; End ; - see the effect of the SELECT * the fROM USER_TABLES the WHERE table_name the LIKE upper('aa_wanglc_test%'); SELECT * FROM USER_indexes WHERE table_name LIKE upper('aa_wanglc_test%'); -- 删除测试表的语句 select 'drop table '|| table_name || ';' from user_tables where table_name like upper('aa_wanglc_test%');
Description result
This procedure can be stored by the other tables to build a table, and can establish the source table primary key index and the like of the object.