优化这段代码:CREATE OR REPLACE PROCEDURE SP_DAILY_BACKUP AS V_SDATE VARCHAR2(8); V_SQL1 VARCHAR2(2000); V_SQL2 VARCHAR2(2000); BEGIN --获取当天日期 select to_char(SYSDATE, 'YYYYMMDD') into V_SDATE from dual; v_sql1 := 'delete from rpt_cust_jyhz_assetcertification where SDATE='V_SDATE; execute immediate v_sql1; ———————————————— v_sql1 := 'insert into rpt_cust_jyhz_assetcertification select '''||V_SDATE||''',col from table_name where SDATE='V_SDATE; execute immediate v_sql1; COMMIT; v_sql2 := 'delete from rpt_cust_rzrq_assetcertification where SDATE='V_SDATE; execute immediate v_sql2; ———————————————— v_sql2 := 'insert into rpt_cust_rzrq_assetcertification select '''||V_SDATE||''',col from table_name where SDATE='V_SDATE; execute immediate v_sql2; COMMIT; end SP_DAILY_BACKUP;
时间: 2023-04-02 21:03:39 浏览: 144
thr_pool.rar_线程池 vc++
优化后的代码如下:
CREATE OR REPLACE PROCEDURE SP_DAILY_BACKUP AS
V_SDATE VARCHAR2(8);
V_SQL1 VARCHAR2(200);
V_SQL2 VARCHAR2(200);
BEGIN
--获取当天日期
SELECT TO_CHAR(SYSDATE, 'YYYYMMDD') INTO V_SDATE FROM DUAL;
--删除 rpt_cust_jyhz_assetcertification 表中当天日期的数据
V_SQL1 := 'DELETE FROM rpt_cust_jyhz_assetcertification WHERE SDATE = :1';
EXECUTE IMMEDIATE V_SQL1 USING V_SDATE;
--将 table_name 表中当天日期的数据插入 rpt_cust_jyhz_assetcertification 表中
V_SQL1 := 'INSERT INTO rpt_cust_jyhz_assetcertification SELECT :1, col FROM table_name WHERE SDATE = :2';
EXECUTE IMMEDIATE V_SQL1 USING V_SDATE, V_SDATE;
--提交事务
COMMIT;
--删除 rpt_cust_rzrq_assetcertification 表中当天日期的数据
V_SQL2 := 'DELETE FROM rpt_cust_rzrq_assetcertification WHERE SDATE = :1';
EXECUTE IMMEDIATE V_SQL2 USING V_SDATE;
--将 table_name 表中当天日期的数据插入 rpt_cust_rzrq_assetcertification 表中
V_SQL2 := 'INSERT INTO rpt_cust_rzrq_assetcertification SELECT :1, col FROM table_name WHERE SDATE = :2';
EXECUTE IMMEDIATE V_SQL2 USING V_SDATE, V_SDATE;
--提交事务
COMMIT;
END SP_DAILY_BACKUP;
这个过程中,我们使用了绑定变量,这样可以提高 SQL 语句的执行效率。同时,我们将相同的代码段合并,减少了代码的重复。
阅读全文