简体   繁体   English

在PL / pgSQL触发函数中使用主机变量

[英]Using host variables in a PL/pgSQL trigger functions

I am trying to figure out how to write a trigger function in PL/pgSQL that can select values into host variables in an Embedded SQL/C program. 我试图弄清楚如何在PL / pgSQL中编写一个触发函数,该函数可以在嵌入式SQL / C程序的主机变量中选择值。 I am new to SQL, so I apologize if my terminology is a bit off. 我是SQL的新手,所以如果我的术语有点过道,我深表歉意。 Basically, on a change to the table, I want the function to trigger a view in order to update the values stored in host variables. 基本上,在更改表时,我希望函数触发视图以更新存储在主机变量中的值。

This is the view I have created and tested to work: 这是我创建并测试可以使用的视图:

EXEC SQL CREATE VIEW idfound AS SELECT id, num FROM newtable WHERE id = 1;

This is how I have called the view, using host variables dbID and dbNum: 这就是使用主机变量dbID和dbNum调用视图的方式:

EXEC SQL SELECT * INTO :dbID, :dbNum FROM idfound;

Here is the trigger I have created: 这是我创建的触发器:

EXEC SQL CREATE idTrigger AFTER INSERT OR UPDATE ON newtable 
FOR EACH ROW EXECUTE PROCEDURE idFunc();

And here is the trigger function I would like to use: 这是我想使用的触发函数:

EXEC SQL CREATE FUNCTION idFunc() RETURNS TRIGGER AS $idTrigger$
    BEGIN
    SELECT * INTO :dbID, :dbNum FROM idfound;
    END;
$idTrigger$ LANGUAGE plpgsql;

For testing purposes, I am using pgAdmin III. 出于测试目的,我使用pgAdmin III。 When I change the function to perform any action that doesn't use the host variables (such as update a value in a predefined row), I can see that it is created. 当我更改函数以执行不使用主机变量的任何操作(例如更新预定义行中的值)时,可以看到它已创建。 However, I am unable to get it to create the trigger or trigger function using the host variables. 但是,我无法使用主机变量来创建触发器或触发器函数。

Right now, I am just trying to figure out how to achieve this in general, so I am aware that my current code doesn't actually do much. 现在,我只是试图找出总体上该如何实现的方法,因此我知道当前的代码实际上并没有做什么。 The general idea is that I want my trigger function to call a predefined view that uses host variables in C. If there is a better way of implementing the trigger, that advice would be greatly appreciated as well. 一般的想法是,我希望我的触发器函数调用一个使用C语言中的宿主变量的预定义视图。如果有更好的实现触发器的方法,那么也将不胜感激。

Trigger functions are executed in the server environment and cannot access the memory space of the client environment, neither for reading nor writing. 触发器功能在服务器环境中执行,无法访问客户端环境的存储空间,无论是读取还是写入。 That's a fundamental point of the client-server architecture, so there's no workaround, this is just not possible. 这是客户端-服务器体系结构的基本要点,因此没有解决方法,这是不可能的。

The closer workable equivalent may be for the trigger to NOTIFY and for the client to LISTEN and consume the notifications and the associated payload. 更接近可行的等效项可能是:触发NOTIFY ,使客户端侦听并使用通知和关联的有效负载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM