我想在PostgreSQL中的函数中使用保存点功能。我读过这个保存点不能在Postgres的函数里面使用。PostgreSQL中的保存功能
但是,当我回滚,我想回滚到一个特定的点,因为我想使用保存点。什么是替代方法呢?
示例代码
CREATE or replace FUNCTION fn_loadData_Subha()
RETURNS BIGINT
AS
$$
DECLARE
batchId BIGINT;
currentTime TIMESTAMP;
processName VARCHAR(20);
BEGIN
-- Getting current date and time
select TIMESTAMP 'NOW' into currentTime;
select 'ETL_Subha' INTO processName;
SAVEPOINT first_savepoint;
-- Inserting new record into batch log table
INSERT INTO TB_HSS_BATCH_LOG
(PROCESS_NAME,START_DATE,STATUS)
SELECT processName,currentTime,'STARTED';
select currval('TB_HSS_BATCH_LOG_id_seq') INTO batchId;
-- Inserting cost data to history table
Insert into tb_hss_procedure_cost_hist1
(HOSP_SYSTEM, HOSP_FACILITY, surgeon_name, procedure_name, department, current_dept_rank, no_of_surgeons, current_imp_cost
, current_med_surg_cost, current_total_cost, annual_volume, sys_pref_cost,load_seq_no, CREATED_AT)
Select
HOSP_SYSTEM, HOSP_FACILITY,surgeon_name,procedure_name,department,current_dept_rank, no_of_surgeons, current_imp_cost
, current_med_surg_cost, current_total_cost, annual_volume, sys_pref_cost, batchId,currentTime
from tb_hss_procedure_cost_stag_in;
RELEASE SAVEPOINT first_savepoint;
RETURN 1;
EXCEPTION
WHEN PLPGSQL_ERROR THEN
RAISE EXCEPTION '% %', SQLERRM, SQLSTATE;
RAISE NOTICE '% %', SQLERRM, SQLSTATE;
RETURN 0;
WHEN OTHERS THEN
RAISE EXCEPTION '% %', SQLERRM, SQLSTATE;
RAISE NOTICE '% %', SQLERRM, SQLSTATE;
RETURN 0;
ROLLBACK TRANSACTION;
END;
$$LANGUAGE plpgsql;
你需要'回滚到first_savepoint':https://www.postgresql.org/docs/current/static/sql-rollback-to.html –
不能使用PostgreSQL的函数内的任何交易相关的SQL语句。 –
回滚到first_savepoint不起作用,因为保存点不能在函数内部使用。我想知道替代保存点.. –