首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >测试PostgreSQL函数

测试PostgreSQL函数
EN

Stack Overflow用户
提问于 2016-06-18 05:10:28
回答 1查看 461关注 0票数 1

我正在将现有的Sybase系统迁移到PostgreSQL,并且在测试我的功能时遇到了困难。例如,我有以下代码

代码语言:javascript
复制
drop function if exists contract_line_ins(bigint, bigint, char(10), date, date, numeric(18, 0), numeric(18, 0), integer, bigint); 

create function contract_line_ins (
  in    _contract_id       bigint,
  in    _product_id        bigint,
  in    _number            char(10),
  in    _start_date        date,
  in    _end_date          date,
  in    _history_access    numeric(18, 0),
  in    _subscription_type numeric(18, 0),
  inout _rows_updated      integer,
  inout _id                bigint
)
as $$
declare
  _locus int = 1;    -- Set this in code to indicate current executing statement in exception
  _at text;          -- Used in exception handling

begin
  insert into contract_line(contract_id, product_id, number, start_date, end_date, history_access, subscription_type)
  values (_contract_id, _product_id, _number, _start_date, _end_date, _history_access, _subscription_type)
  returning _id;

  get diagnostics _rows_updated = row_count;

-- Exception handling
  exception when others then
    get stacked diagnostics _at = PG_EXCEPTION_CONTEXT;
    raise notice E'EXCEPTION\nError:   %\nMessage: %\nLocus:   %\nAt:      %', SQLSTATE, SQLERRM, _locus, _at;
end;
$$ language plpgsql;

我理解,在这种情况下,拥有两个返回值都是超乎寻常的,但除非绝对必要,否则我被要求不要更改参数。

我编写了以下测试代码

代码语言:javascript
复制
do language plpgsql $$
declare
  contract_id       bigint = 1;
  product_id        bigint = 2;
  number            char(10) = 'CONTRACT';
  start_date        date = '20160101';
  end_date          date = '20161231';
  history_access    numeric(18, 0) = 3;
  subscription_type numeric(18, 0) = 4;
  rows_updated      int;
  id                bigint;
begin
  perform contract_line_ins(contract_id, product_id, number, start_date, end_date, history_access, subscription_type, rows_updated, id);

  raise notice E'row count % id %', rows_updated, id;
end
$$

当我执行这个测试时,我会得到以下内容:

代码语言:javascript
复制
[2016-06-18 05:55:17] EXCEPTION
Error:   42601
Message: query has no destination for result data
Locus:   1
At:      PL/pgSQL function contract_line_ins(bigint,bigint,character,date,date,numeric,numeric,integer,bigint) line 7 at SQL statement
SQL statement "SELECT contract_line_ins(contract_id, product_id, number, start_date, end_date, history_access, subscription_type, rows_updated, id)"
PL/pgSQL function inline_code_block line 13 at PERFORM
[2016-06-18 05:55:17] row count <NULL> id <NULL>
[2016-06-18 05:55:17] completed in 43ms

我不明白的是:

  1. 为什么“查询没有结果数据的目的地”消息?我以为表演是为了防止
  2. 异常发生在我的测试代码中,但正在函数中引发。我原以为这个函数只会报告在自己身体中发生的异常。为什么会发生这种情况?

虽然我发现PostgreSQL文档非常优秀,但我还是找不出如何正确地做到这一点。

收到的任何建议都很感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-06-18 05:44:30

Q1

我认为这是returning _id位提供了一个例外。因为INSERT ... RETURNING应该以列列表结束,而不是变量名。如果要插入行,列id值为_id变量-将其更改为returning id INTO _id

Q2

我相信函数中会出现错误。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37893475

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档