删除以PK为参数的行的功能

时间:2016-10-13 10:31:19

标签: sql postgresql stored-procedures plpgsql

我想创建一个没有返回值的函数来删除基于主键bill_key的名为bill的表中的记录。该列名为bill_key。我的代码是:

CREATE OR REPLACE FUNCTION public.delete_bill(bill_key_to_delete bigint)
RETURNS void AS
$BODY$
begin
  DELETE FROM bill
  WHERE "bill_key" = bill_key_to_delete;
  return;
end;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;

我称之为功能:

select delete_bill(2);

但我得到的错误信息是:

select delete_bill(2)
        ERROR:  column reference "bill_key" is ambiguous
        LINE 2: WHERE "bill_key" = bill_key_to_delete
                      ^
        DETAIL:  It could refer to either a PL/pgSQL variable or a table column.
        QUERY:  DELETE FROM bill
        WHERE "bill_key" = bill_key_to_delete
        CONTEXT:  PL/pgSQL function delete_bill(bigint) line 2 at SQL statement

1 个答案:

答案 0 :(得分:0)

尝试使用表名的别名:

DELETE FROM bill b
WHERE b."bill_key" = bill_key_to_delete;

如果函数返回void类型,则应使用PERFORM执行函数

PERFORM delete_bill(2);