PostgreSQL和Pg​​Admin设置会话变量以查询结果

时间:2019-01-17 11:06:17

标签: postgresql

为什么会出现此错误?

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 "...
                                 ^

1 个答案:

答案 0 :(得分:1)

因为只能在SET命令中使用常量。

尝试一下:

SELECT set_config('my.vars.id', "Name", FALSE)
FROM "Client" WHERE "ID" = 1;