在用单行结果分配查询时,错误不是已知变量

发布于 2025-01-27 15:54:42 字数 1267 浏览 2 评论 0原文

我有三张表:处方,项目和订单。 处方表和项目表都有一个不可废的列,称为价格。 在订单表中,我有多个列。

  1. 类型 - “处方”或“项目”金额的枚举
  2. 的不可用int,以
  3. - 我试图创建触发器的默认值

检查订单类型是处方还是订单,并将价格乘以乘以金额的价格订单表。如果价格是手动插入的,我也会丢下错误,因为我只希望在服务器端计算它。

到目前为止,我的代码是这个。

CREATE OR REPLACE FUNCTION trg_orders_check()
  RETURNS trigger
  LANGUAGE plpgsql AS
$func$
BEGIN
  IF NEW.type = 'prescription' AND NEW.prescription_id IS NULL THEN
    RAISE EXCEPTION 'Must include prescription_id';
  ELSIF NEW.TYPE = 'prescription' THEN
    base_price := SELECT price FROM prescriptions
    WHERE prescription_id = NEW.prescription_id;
    INSERT INTO NEW (price) VALUES (base_price * NEW.amount);
  ELSIF NEW.TYPE = 'item' AND NEW.item_id IS NULL THEN
    RAISE EXCEPTION 'Must include item_id';
  ELSIF NEW.TYPE = 'prescription' THEN
    base_price float := SELECT price FROM orders
    WHERE item_id = NEW.item_id;
    INSERT INTO NEW (price) VALUES (base_price * NEW.amount);
  ELSE
    RAISE EXCEPTION 'Type must be either prescription or item';
  END IF;
END
$func$;

现在我遇到了错误 “ base_price”不是已知的变量

如果我执行base_price float:= ... expression

我想知道是什么导致该错误,还有更多从广义上讲,如果这是创建上面概述的触发器的正确​​方法。任何帮助将不胜感激。

I have three tables: prescriptions, items, and orders.
The prescriptions and items table both have a non nullable column called price.
In the orders table, I have multiple columns.

  1. type - enum of either 'prescription' or 'item'
  2. amount - non nullable int with default of 1
  3. price

I'm trying to create trigger that checks whether the order type is prescription or order, and inserts the price multiplied by the amount into the orders table. I'm also throwing an error if the price is manually inserted, because I only want it to be calculated on the server side.

My code so far is this.

CREATE OR REPLACE FUNCTION trg_orders_check()
  RETURNS trigger
  LANGUAGE plpgsql AS
$func$
BEGIN
  IF NEW.type = 'prescription' AND NEW.prescription_id IS NULL THEN
    RAISE EXCEPTION 'Must include prescription_id';
  ELSIF NEW.TYPE = 'prescription' THEN
    base_price := SELECT price FROM prescriptions
    WHERE prescription_id = NEW.prescription_id;
    INSERT INTO NEW (price) VALUES (base_price * NEW.amount);
  ELSIF NEW.TYPE = 'item' AND NEW.item_id IS NULL THEN
    RAISE EXCEPTION 'Must include item_id';
  ELSIF NEW.TYPE = 'prescription' THEN
    base_price float := SELECT price FROM orders
    WHERE item_id = NEW.item_id;
    INSERT INTO NEW (price) VALUES (base_price * NEW.amount);
  ELSE
    RAISE EXCEPTION 'Type must be either prescription or item';
  END IF;
END
$func$;

Right now I'm getting the error
"base_price" is not a known variable

I get the same error if I do base_price float := ...expression

I'm wondering what is causing that error, and more broadly if this is the correct approach for creating the trigger I outlined above. Any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海拔太高太耀眼 2025-02-03 15:54:42

不支持此可变声明的语法。您应该使用

DECLARE x int;
BEGIN
  x := 10;
END

This syntax for variable declaration is not supported. You should to use DECLARE statement before block.

DECLARE x int;
BEGIN
  x := 10;
END
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文