将新的 Table 引用为 new_table 对触发器函数 Postgresql 不可见
referencing new Table as new_table not visible to the trigger function Postgresql
我是 Postgresql 的新手,所以这个问题对你们来说可能很愚蠢!
我想在触发器函数中使用引用的 tables,但有些函数似乎无法访问引用的别名 (new_table
) :
CREATE FUNCTION Function_Name()
RETURNS TRIGGER AS
$$
BEGIN
SELECT tbl.some_field INTO new_table.other_field
from some_table as tbl;
return null;
END;
$$ LANGUAGE PLPGSQL;
我有这个错误:
"new_table.other_field" is not a known variable
这里是触发代码:
CREATE TRIGGER Trigger_Name
AFTER INSERT
ON Table_Name
REFERENCING NEW TABLE AS new_table
FOR EACH ROW
EXECUTE FUNCTION Function_Name();
应该先执行函数代码,然后再执行触发器代码,那么函数代码如何访问触发器定义中引用的别名later?
以及如何访问函数中引用的 table 别名?
注意:在我的示例中,我尝试使用别名,因此当我使用 NEW 代替 new_table 时函数创建成功!
问题是我正在尝试使用别名在 NEW
table 中设置数据,但是为此我应该使用原始名称 NEW
而不是引用的别名 new_table
..
引用的别名 new_table
只能用于从 FROM CLAUSE
、joins
和 WHERE CLAUSE
中获取数据,其中不更改数据引用的 table.
更新:
这是我测试的例子:
create table test_table2(
id int primary key,
name varchar(255)
)
create table test_table(
id int primary key,
name varchar(255)
)
create or replace function F_test_table()
returns trigger as $$
begin
insert into test_table2(id, name)
select id, name from new_table;
return null;
end;
$$ LANGUAGE PLPGSQL;
drop trigger if exists tr_test_table ON test_table;
create trigger tr_test_table
AFTER INSERT
ON test_table
REFERENCING NEW TABLE AS new_table
for each row ------- row level trigger ---------
EXECUTE FUNCTION F_test_table();
insert into test_table
values(1, '11111')
select * from test_table2
注意触发器将数据插入 test_table2
我是 Postgresql 的新手,所以这个问题对你们来说可能很愚蠢!
我想在触发器函数中使用引用的 tables,但有些函数似乎无法访问引用的别名 (new_table
) :
CREATE FUNCTION Function_Name()
RETURNS TRIGGER AS
$$
BEGIN
SELECT tbl.some_field INTO new_table.other_field
from some_table as tbl;
return null;
END;
$$ LANGUAGE PLPGSQL;
我有这个错误:
"new_table.other_field" is not a known variable
这里是触发代码:
CREATE TRIGGER Trigger_Name
AFTER INSERT
ON Table_Name
REFERENCING NEW TABLE AS new_table
FOR EACH ROW
EXECUTE FUNCTION Function_Name();
应该先执行函数代码,然后再执行触发器代码,那么函数代码如何访问触发器定义中引用的别名later?
以及如何访问函数中引用的 table 别名?
注意:在我的示例中,我尝试使用别名,因此当我使用 NEW 代替 new_table 时函数创建成功!
问题是我正在尝试使用别名在 NEW
table 中设置数据,但是为此我应该使用原始名称 NEW
而不是引用的别名 new_table
..
引用的别名 new_table
只能用于从 FROM CLAUSE
、joins
和 WHERE CLAUSE
中获取数据,其中不更改数据引用的 table.
更新:
这是我测试的例子:
create table test_table2(
id int primary key,
name varchar(255)
)
create table test_table(
id int primary key,
name varchar(255)
)
create or replace function F_test_table()
returns trigger as $$
begin
insert into test_table2(id, name)
select id, name from new_table;
return null;
end;
$$ LANGUAGE PLPGSQL;
drop trigger if exists tr_test_table ON test_table;
create trigger tr_test_table
AFTER INSERT
ON test_table
REFERENCING NEW TABLE AS new_table
for each row ------- row level trigger ---------
EXECUTE FUNCTION F_test_table();
insert into test_table
values(1, '11111')
select * from test_table2
注意触发器将数据插入 test_table2