云数据库 GAUSSDB-函数支持自治事务

时间:2024-11-02 18:47:50

函数支持自治事务

自治事务可以在函数中定义,标识符为PRAGMA AUTONOMOUS_TRANSACTION,其余语法与函数语法相同,示例如下。

create table t4(a int, b int, c text);

CREATE OR REPLACE function autonomous_32(a int ,b int ,c text) RETURN int AS 
DECLARE 
	PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
	insert into t4 values(a, b, c);
	return 1;
END;
/
CREATE OR REPLACE function autonomous_33(num1 int) RETURN int AS 
DECLARE 
	num3 int := 220;
	tmp int;
	PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
	num3 := num3/num1;
	return num3;
EXCEPTION
	WHEN division_by_zero THEN
		select autonomous_32(num3, num1, sqlerrm) into tmp;
		return 0;
END;
/

select autonomous_33(0);

select * from t4;
support.huaweicloud.com/distributed-devg-v2-gaussdb/gaussdb-12-0567.html