如何在pgAdmin中声明变量

编程入门 行业动态 更新时间:2024-10-27 12:28:51
本文介绍了如何在pgAdmin中声明变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

\set message_id soifsdaofisd.gmail; 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.

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

推荐答案

\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中声明变量

本文发布于:2023-10-13 20:48:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1489035.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   声明   如何在   pgAdmin

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!