如何在pgAdmin中声明变量 [英] How do I declare variables in pgAdmin

查看:253
本文介绍了如何在pgAdmin中声明变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在psql终端中,则可以声明并使用像这样的变量:

If I am in the psql terminal then I can declare and use a variable like this:

\set message_id soifsdaofisd.gmail.com;
select * from emails where message_id = ':message_id';

我该如何在pgAdmin中做到这一点?

How can I do this in pgAdmin?

我在pgAdmin中尝试此操作时都会收到错误消息:

I get an error when ever I try this in pgAdmin:

错误:"CALAdkA4YC0"或附近的语法错误第3行:设置message_id soifsdaofisd.gmail.com.

ERROR: syntax error at or near "CALAdkA4YC0" LINE 3: set message_id soifsdaofisd.gmail.com.

推荐答案

\set是psql(交互式命令行终端)的功能,在pgAdmin中不可用.

\set is a feature of psql (the interactive command line terminal) and not available in pgAdmin.

PostgreSQL通常在普通SQL中不使用变量.您可以在匿名块中使用 PL/pgSQL代码( DO 语句)或在

PostgreSQL does not normally use variables in plain SQL. You would use PL/pgSQL code in an anonymous block (DO statement) or in a function.

但是,您可以(ab)使用

However, you can (ab)use customized options, for server-side "variables", independent of which client you are using at the moment:

SET foo.test = 'SELECT bar FROM baz';
SELECT current_setting('foo.test');

此相关答案的详细信息:
PostgreSQL中的用户定义变量

Details in this related answer:
User defined variables in PostgreSQL

还有 pgScript ,这是pgAdmin查询工具中的本地脚本扩展,您可以在其中使用与psql相当的本地变量.每个文档:

And there is also pgScript, a local scripting extension in the pgAdmin query tool, where you can use local variables, comparable to what you can do in psql. Per documentation:

您可以通过从以下选项中选择执行pgScript"来运行pgScript脚本: 查询菜单,而不是执行,或按执行pgScript 工具栏按钮,或者按F6功能键.

You can run pgScript scripts by selecting Execute pgScript from the Query menu instead of Execute, or you press the Execute pgScript toolbar button, or you press the F6 function key.

pgScript脚本语言参考中的详细信息.

这篇关于如何在pgAdmin中声明变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆