Postgresql和PgAdmin SIMPLE变量

时间:2019-01-17 08:55:41

标签: postgresql pgadmin

我不明白为什么在pgAdmin中创建变量必须如此复杂,但是有人可以帮助我解决这个问题。我只想从记录中创建一个Name的基本变量。然后,我想再次搜索该Name。因此,这是我需要的基本示例:

VAR1 = SELECT "Name" 
FROM "Client"
WHERE "ID" = '1'; // This select will return 'Peter'

SELECT * FROM "Client" WHERE "Name" = VAR1;

我已经尝试了所有这些方法。创建临时表:

CREATE TEMP TABLE VAR1 AS VALUES (SELECT "Name" FROM "Client" WHERE "UUID" = 1;);
SELECT * FROM "Client" WHERE  Name = VAR1.column1;
...
ERROR:  syntax error at or near "SELECT"
LINE 1: CREATE TEMP TABLE VAR1 AS VALUES (SELECT "Name" FROM "Client...
                                          ^

这在pgAdmin中不起作用,仅在psql中起作用:

\set VAR1 (SELECT "Name" FROM "Client" WHERE "ID" = '1')
SELECT * FROM "Client" WHERE name = :VAR1;

这也不起作用:

set session my.vars.id = SELECT "Name" FROM "Client" WHERE "ID" = 1;
SELECT * FROM "Client" WHERE "Name" = current_setting('my.vars.id');
...
ERROR:  syntax error at or near "SELECT"
LINE 1: set session my.vars.id = SELECT "Name" FROM "Client" WHERE "...
                                 ^

目前这行不通,但是期望我为一个简单的VAR1 = x编写所有这些代码是荒谬的。即使您可以使用此功能,也请不要将其作为答案:

DO
$$
DECLARE
  VAR1 text = (SELECT "Name" FROM "Client" WHERE "UUID" = 1);
BEGIN
  RETURN (SELECT * FROM "Client" WHERE "Name" = VAR1);
END;
$$
LANGUAGE plpgsql;
...
ERROR:  RETURN cannot have a parameter in function returning void
LINE 6:   RETURN (SELECT * FROM "Client" WHERE "Name" = VAR1);
                 ^

使用WITH不起作用,因为您只能在WITH块正下方使用一次该变量。我希望能够在整个脚本中使用该变量。

为什么必须这么复杂? VAR1 = x

我读了许多其他答案(那是我完成所有测试的地方)。因此,除非它们提供其他功能,否则请不要引用它们。

0 个答案:

没有答案