在 postgresql 中,执行 INSERT INTO RETURNING 时返回规则中函数的结果

发布于 2024-09-02 07:10:28 字数 635 浏览 2 评论 0原文

我有一个包含多个字段的视图。

当我INSERT INTO视图时,我运行一个基于INSERT参数的函数。该函数返回一个值。

如何从规则中检索值?

INSERT RETURNING 给我:

错误:无法对关系“full_subntes”执行 INSERT RETURNING
提示:您需要一个带有 RETURNING 子句的无条件 ON INSERT DO INSTEAD 规则。

示例:

CREATE TABLE test (
    a VARCAHR primary key,
    b VARCHAR,
);

CREATE VIEW test_v AS SELECT * FROM test;

CREATE OR REPLACE RULE Test_v_Insert AS ON INSERT TO Test_v
DO INSTEAD (
    SELECT myFunction('param');
);

INSERT INTO test_v(a, b) VALUES ('a', 'b') RETURNING a, b;

然后我收到上述错误。

I have a View that has several fields.

When i INSERT INTO a view I run a function based on INSERT parametrs. The function returns a value.

How can I retrieve The value from rule?

INSERT RETURNING Gives me:

ERROR: cannot perform INSERT RETURNING on relation "full_subntes"
HINT: You need an unconditional ON INSERT DO INSTEAD rule with a RETURNING clause.

Example:

CREATE TABLE test (
    a VARCAHR primary key,
    b VARCHAR,
);

CREATE VIEW test_v AS SELECT * FROM test;

CREATE OR REPLACE RULE Test_v_Insert AS ON INSERT TO Test_v
DO INSTEAD (
    SELECT myFunction('param');
);

INSERT INTO test_v(a, b) VALUES ('a', 'b') RETURNING a, b;

Then I get an error described above.

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

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

发布评论

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

评论(2

摘星┃星的人 2024-09-09 07:10:28

这是一个例子。

首先,我们创建一个测试表:

CREATE TABLE test (a integer, b varchar, primary key (a));

然后,我们创建一个视图:

CREATE OR REPLACE VIEW test_view AS SELECT * FROM test;

接下来,创建更新规则:

CREATE OR REPLACE RULE rl_test_view_update AS
    ON UPDATE TO test_view DO INSTEAD
    UPDATE test SET a = NEW.a, b = NEW.b
    WHERE test.a = old.a AND test.b = old.b;

最后,这是插入规则:

CREATE OR REPLACE RULE rl_test_view_insert AS
    ON INSERT TO test_view DO INSTEAD
    INSERT INTO test VALUES (NEW.a, NEW.b)
    RETURNING test.*;

现在您可以插入一些测试数据:

INSERT INTO test_view (a, b) VALUES (1,'John Doe') RETURNING a;

并检查插入的元组:

SELECT * FROM test_view;

Here is an example.

First, we create a test table:

CREATE TABLE test (a integer, b varchar, primary key (a));

Then, we create a view:

CREATE OR REPLACE VIEW test_view AS SELECT * FROM test;

Next, the update rule is created:

CREATE OR REPLACE RULE rl_test_view_update AS
    ON UPDATE TO test_view DO INSTEAD
    UPDATE test SET a = NEW.a, b = NEW.b
    WHERE test.a = old.a AND test.b = old.b;

And finally, here is the insert rule:

CREATE OR REPLACE RULE rl_test_view_insert AS
    ON INSERT TO test_view DO INSTEAD
    INSERT INTO test VALUES (NEW.a, NEW.b)
    RETURNING test.*;

Now you can insert some test data:

INSERT INTO test_view (a, b) VALUES (1,'John Doe') RETURNING a;

and check the tuples inserted:

SELECT * FROM test_view;
毅然前行 2024-09-09 07:10:28

为了更新 Postgres 中的视图,您需要定义一个规则,告诉它更新基表。听起来您还没有创建规则。 http://www.postgresql.org/docs/current/interactive/规则更新.html

In order to update a view in Postgres, you need to define a rule telling it to update the base table instead. It doesn't sound like you've created a rule yet. http://www.postgresql.org/docs/current/interactive/rules-update.html

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