博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PostgreSQL Oracle 兼容性之 - DBMS_OUTPUT.PUT_LINE
阅读量:7098 次
发布时间:2019-06-28

本文共 1976 字,大约阅读时间需要 6 分钟。

标签

PostgreSQL , Oracle , DBMS_OUTPUT.PUT_LINE , raise , notice


背景

在函数、存储过程中需要进行一些debug,输出一些过程变量的值时,PG中使用raise notice可以非常方便的得到。

Oracle

put_line在存储过程、函数中通常被用于调试,输出一些变量,时间值。

CREATE FUNCTION dept_salary (dnum NUMBER) RETURN NUMBER IS     CURSOR emp_cursor IS        SELECT sal, comm FROM emp WHERE deptno = dnum;     total_wages    NUMBER(11, 2) := 0;     counter        NUMBER(10) := 1;  BEGIN       FOR emp_record IN emp_cursor LOOP        emp_record.comm := NVL(emp_record.comm, 0);        total_wages := total_wages + emp_record.sal           + emp_record.comm;        DBMS_OUTPUT.PUT_LINE('Loop number = ' || counter ||            '; Wages = '|| TO_CHAR(total_wages));  /* Debug line */        counter := counter + 1; /* Increment debug counter */     END LOOP;     /* Debug line */     DBMS_OUTPUT.PUT_LINE('Total wages = ' ||       TO_CHAR(total_wages));      RETURN total_wages;    END dept_salary;

Assume the EMP table contains the following rows:

EMPNO          SAL     COMM     DEPT  -----        ------- -------- -------  1002           1500      500      20  1203           1000               30  1289           1000               10  1347           1000      250      20

Assume the user executes the following statements in SQL*Plus:

SET SERVEROUTPUT ON  VARIABLE salary NUMBER;  EXECUTE :salary := dept_salary(20);

The user would then see the following information displayed in the output pane:

Loop number = 1; Wages = 2000  Loop number = 2; Wages = 3250  Total wages = 3250    PL/SQL procedure successfully executed.

PostgreSQL

PostgreSQL plpgsql存储过程,其他存储过程语言同样有类似的用法(plr, plpython, plperl, pltcl, pljava, pllua等)

以plpgsql为例,使用raise notice即可用于输出调试信息,例如

postgres=# do language plpgsql $$  declare  begin    raise notice 'now: %, next time: %', now(), now()+interval '1 day';  end;  $$;    输出    NOTICE:  now: 2018-08-18 16:15:30.209386+08, next time: 2018-08-19 16:15:30.209386+08  DO  Time: 0.335 ms

捕获状态变量信息

GET STACKED DIAGNOSTICS ....    GET DIAGNOSTICS ...

参考

转载地址:http://gjeql.baihongyu.com/

你可能感兴趣的文章
Session机制详解
查看>>
【转】使用PHP导入和导出CSV文件
查看>>
面向对象概念思想再理解 2.0
查看>>
VS code 格式化插件, 仅需一步, 无须配置
查看>>
EL表达式的一些知识
查看>>
web 中的认证方式
查看>>
node模块之path——path.join和path.resolve的区别
查看>>
SDNU 1292.圣诞老人
查看>>
BZOJ 3629 约数和定理+搜索
查看>>
ClientDemo
查看>>
mysql
查看>>
命令行的快速入门【第一天】
查看>>
Xml 丶json丶 C/S KVO 数据库 SQL 数据持久化 复杂对象 集合视图综合
查看>>
开源监控系统整合Nagios+Cacti+Nconf详解
查看>>
对于深入响应式原理的深刻理解
查看>>
初级AD域渗透系列
查看>>
题解 P1036 【选数】
查看>>
【算法学习笔记】25.贪心法 均分纸牌问题的分析
查看>>
width:100vh有感而发
查看>>
模仿京东淘宝的物流跟踪模板样式
查看>>