1. 复制表结构及其数据:
create table table_name_new as select * from table_name_old
2. 只复制表结构(后面加where条件):
create table table_name_new as select * from table_name_old where 1=0;
或者:
create table table_name_new like table_name_old
3. 只复制表数据:
如果两个表结构一样:
insert into table_name_new select * from table_name_old
如果两个表结构不一样:
insert into table_name_new(column1,column2...) select column1,column2... from table_name_old
方式一:v_temp_hour:='21';
方式二:select to_char(sysdate,'hh24') into v_temp_hour from dual;
说明:方式一是直接给字符串变量赋值,方式二是通过查询sql给变量赋值
注意:需要使用 execute immediate 执行sql
注意:
if 条件 then
逻辑处理
elsif 条件 then
逻辑处理
elsif 条件 then
逻辑处理
else
逻辑处理
end if;
6、存储过程实例
CREATE OR REPLACE PROCEDURE from_celltemp_to_cell IS v_temp_hour varchar2(20); v_temp_week varchar2(20); v_table_name_hour varchar2(50); v_table_name_week varchar2(50); v_cell_max_time varchar2(20); v_sysdate_time varchar2(20); v_sql varchar2(200); v_insert_cell_sql varchar2(2000); v_truncate_cell_sql varchar2(1000); v_truncate_cell_temp_sql varchar2(2000); BEGIN -- 查询当前系统时间,获得小时 select to_char(sysdate,'hh24') into v_temp_hour from dual; -- 根据小时判断迁移哪张分区表 if v_temp_hour='00' or v_temp_hour='01' or v_temp_hour='02' or v_temp_hour='03' then v_table_name_hour := 'M_CELL_TEMP_part20_23'; elsif v_temp_hour='04' or v_temp_hour='05' or v_temp_hour='06' or v_temp_hour='07' then v_table_name_hour := 'M_CELL_TEMP_part00_03'; elsif v_temp_hour='08' or v_temp_hour='09' or v_temp_hour='10' or v_temp_hour='11' then v_table_name_hour := 'M_CELL_TEMP_part04_07'; elsif v_temp_hour='12' or v_temp_hour='13' or v_temp_hour='14' or v_temp_hour='15' then v_table_name_hour := 'M_CELL_TEMP_part08_11'; elsif v_temp_hour='16' or v_temp_hour='17' or v_temp_hour='18' or v_temp_hour='19' then v_table_name_hour := 'M_CELL_TEMP_part12_15'; elsif v_temp_hour='20' or v_temp_hour='21' or v_temp_hour='22' or v_temp_hour='23' then v_table_name_hour := 'M_CELL_TEMP_part16_19'; else dbms_output.put_line('error!'); end if;
-- 查询当前系统时间,获得星期几 select replace(to_char(sysdate,'day','nls_date_language=american'),' ','') into v_temp_week from dual; -- 根据星期几判断删除哪张分区表 if v_temp_week='monday' then v_table_name_week := 'M_CELL_total_mon'; elsif v_temp_week='tuesday' then v_table_name_week := 'M_CELL_total_tue'; elsif v_temp_week='wednesday' then v_table_name_week := 'M_CELL_total_wed'; elsif v_temp_week='thursday' then v_table_name_week := 'M_CELL_total_thu'; elsif v_temp_week='friday' then v_table_name_week := 'M_CELL_total_fri'; elsif v_temp_week='saturday' then v_table_name_week := 'M_CELL_total_sat'; elsif v_temp_week='sunday' then v_table_name_week := 'M_CELL_total_sun'; else dbms_output.put_line('error!'); end if;
-- 查看cell表某个分区的日期是否是上周数据 --cell表最大时间 v_sql:='select to_char(max(createtime),''yyyy-mm-dd'') from m_cell partition('||v_table_name_week||')'; EXECUTE IMMEDIATE v_sql into v_cell_max_time ; --系统时间 select to_char(sysdate,'yyyy-mm-dd') into v_sysdate_time from dual;
if v_cell_max_time is not null and v_cell_max_time!=v_sysdate_time then -- 删除cell表中的某一个分区的数据 v_truncate_cell_sql:='ALTER TABLE m_cell TRUNCATE PARTITION '||v_table_name_week; EXECUTE IMMEDIATE v_truncate_cell_sql; end if; -- 往cell表中迁移数据 v_insert_cell_sql:='insert into m_cell (ttime, service_type, ne_name, lac, ci, noservicetime, old_value, current_value, nosignaltime, old_rejtimes, old_successtimes, old_successrate, rejtimes, successtimes, successrate, monitor_notes, param_extral, createtime, id, state_type, monitor_type, confirm_time, cancel_time, keyvalue) select ttime, service_type, ne_name, lac, ci, noservicetime, old_value, current_value, nosignaltime, old_rejtimes, old_successtimes, old_successrate, rejtimes, successtimes, successrate, monitor_notes, param_extral, createtime, sys_guid(), state_type, monitor_type, confirm_time, cancel_time, replace(to_char(createtime,''day'',''nls_date_language=american''),'' '','''') from m_cell_temp partition('||v_table_name_hour||')'; EXECUTE IMMEDIATE v_insert_cell_sql; -- 删除cell_temp表中的某一个分区的数据 v_truncate_cell_temp_sql:='ALTER TABLE m_cell_temp TRUNCATE PARTITION '||v_table_name_hour; EXECUTE IMMEDIATE v_truncate_cell_temp_sql; commit; END from_celltemp_to_cell; |