现在介绍一种在Postgre数据库中调试触发器的方法。
假设数据库中有表TableA,其下面有一个触发器TriggerA。
在SqlServer数据库中使用insert into tableA values(...)的方式,通过SqlServer的管理工具直接就能够调试触发器。在Postgre数据库中可以使用raise语句通过输出日志来达到相同的功能。
raise用于函数中打印输出。
raise的语法为:raise notice 'this is raise test %',param;
创建一个函数raise_test,如下所示:
create or replace function raise_test() returns text language plpgsql as $$
declare
param1 text:= 'one';
param2 text:= 'two';
begin
raise notice '测试使用raise , 参数1 : % ,参数2: %',param1,param2;
return param1;
end;
$$;
执行SQL脚本,创建函数,如下图所示:

执行函数脚本,如下图所示:
select raise_test();
执行结果如下图所示:

日志打印在消息选项卡中。
利用此方法可以在触发器、函数、存储过程中加入raise notice来输出各种参数变量,以达到调试的效果。
网友评论