Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Postgresql 在SQL中使用变量_Postgresql - Fatal编程技术网

Postgresql 在SQL中使用变量

Postgresql 在SQL中使用变量,postgresql,Postgresql,这里是PostgreSQL新手。对不起,这可能很琐碎 本质上,我需要从本地表中获取任意值,然后在SELECT I fire的WHERE子句中对远程表使用它,以便返回我将插入回本地表的行: “提供远程表中f1的时间戳值比同一表的本地版本中f1的最大时间戳值新的所有行” 像这样的,sorta: DECLARE @variable timestamp; @variable = SELECT MAX(f1) from Foo; INSERT INTO Foo SELECT * FROM (dblink

这里是PostgreSQL新手。对不起,这可能很琐碎

本质上,我需要从本地表中获取任意值,然后在SELECT I fire的WHERE子句中对远程表使用它,以便返回我将插入回本地表的行:

“提供远程表中f1的时间戳值比同一表的本地版本中f1的最大时间戳值新的所有行”

像这样的,sorta:

DECLARE @variable timestamp;
@variable = SELECT MAX(f1) from Foo;

INSERT INTO Foo SELECT * FROM (dblink('conn', 'select * from RemoteFoo WHERE f1 > @variable') as
t
( f1 int, f2 timestamp)

在postgreSQL中如何实现这一点?

既然您必须通过dblink退出,我可能会将其封装在函数中

CREATE FUNCTION get_remote_foos() RETURNS int ...
使用plpgsql,您可以或多或少地在示例中编写代码。上面的签名假设您想要返回插入的行数。手册中提供了plgsql的全部细节-请注意该部分中的quote_ident和quote_literal函数。时间戳并不重要,但在其他情况下会很有用