PGAdmin 中的 PostgreSQL 语法错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1899123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 22:23:41  来源:igfitidea点击:

PostgreSQL Syntax error in PGAdmin

postgresqlplpgsql

提问by user1167541

I am new to PostgreSQL and am using the query tool in PGAdmin. I'm trying to run pgsql queries that use variables, but I can't seem to get the syntax right.

我是 PostgreSQL 的新手,正在使用 PGAdmin 中的查询工具。我正在尝试运行使用变量的 pgsql 查询,但我似乎无法获得正确的语法。

Here's a sample query that gives a syntax error:

这是一个提供语法错误的示例查询:

DECLARE
  num INTEGER;

BEGIN

  num := 3;
  PRINT num;

END;


Update:
Ok, let me try and explain. I come from a SQL server background. In the management studio, I can open a query window and play with (T)-SQL queries.

更新:
好的,让我试着解释一下。我来自 SQL 服务器背景。在管理工作室中,我可以打开一个查询窗口并使用 (T)-SQL 查询。

For example, I can write something like this:

例如,我可以这样写:

DECLARE @num INT
SET @num = 3
SELECT @num

I know this is a dumb example, but I'm just trying to declare a variable and do something with it. I'm trying to familiarise myself with PL/PGSQL.

我知道这是一个愚蠢的例子,但我只是想声明一个变量并用它做一些事情。我正在尝试熟悉 PL/PGSQL。



Update, again:
It's me again. I'm trying the script below and get a "[ERROR ] 7.0-2: syntax error, unexpected character". Is this meant to work in PGAdmin?

再次更新:又
是我。我正在尝试下面的脚本并得到一个“[错误] 7.0-2:语法错误,意外的字符”。这是否意味着在 PGAdmin 中工作?

DECLARE
  num INTEGER;

BEGIN

  num := 3;
  RAISE NOTICE '%', num;

END;

回答by user1167541

You can use the do statement. For example:

您可以使用 do 语句。例如:

do $$
declare 
  num integer := 10;
begin

    RAISE INFO 'VARIABLE: %', num;

end;
$$language plpgsql;

When you use pgadmin you have to use the button EXECUTE QUERY instead of Execute pdScript, as it is explained here:

当您使用 pgadmin 时,您必须使用按钮 EXECUTE QUERY 而不是 Execute pdScript,如下所述:

http://postgresql.1045698.n5.nabble.com/PgAmin3-Anonymous-code-block-can-t-be-executed-by-pressing-quot-Execute-PG-script-quot-button-td5771073.html

http://postgresql.1045698.n5.nabble.com/PgAmin3-Anonymous-code-block-can-t-be-executed-by-pressing-quot-Execute-PG-script-quot-button-td5771073.html

The documentation for do statements is here:

do 语句的文档在这里:

http://www.postgresql.org/docs/9.3/static/sql-do.html

http://www.postgresql.org/docs/9.3/static/sql-do.html

回答by filiprem

Just to rephrase and "concretize" what others say: There are no inline proceduresin PostgreSQL. There is also no PRINT statement. You have to:

只是重新表述和“具体化”其他人所说的话:PostgreSQL中没有内联过程。也没有 PRINT 语句。你必须:

CREATE OR REPLACE FUNCTION test() RETURNS void AS $$
DECLARE
  num INTEGER;

BEGIN

  num := 3;
  RAISE NOTICE '%', num;

END;
$$ LANGUAGE plpgsql;

SELECT test();

回答by Edmund

If you're trying to print out num(say, for debugging), you could try:

如果你想打印出来num(比如,为了调试),你可以尝试:

RAISE NOTICE '%', num;

http://www.postgresql.org/docs/8.4/static/plpgsql-errors-and-messages.html

http://www.postgresql.org/docs/8.4/static/plpgsql-errors-and-messages.html

PRINTdoesn't mean anything in PL/pgSQL.

PRINT在 PL/pgSQL 中没有任何意义。

回答by Edmund

I have no idea what you are trying to achieve. PostgreSQL doesn't support this kind of syntax. Similar keywords (except PRINT?!) are in PL/pgSQL which is procedural language for building FUNCTIONS, not for writing stand-alone SQL queries.

我不知道你想达到什么目的。PostgreSQL 不支持这种语法。类似的关键字(除了 PRINT?!)在 PL/pgSQL 中,它是用于构建 FUNCTIONS 的过程语言,而不是用于编写独立的 SQL 查询。

回答by Milen A. Radev

Postgres doesn't support anything like that by itself (yet). psql(the official command line client) has some rudimentary scripting.

Postgres 本身不支持类似的东西()。psql(官方命令行客户端)有一些基本的脚本。

The best option for you is pgAdmin which already has scriptingbuilt-in.

对您来说最好的选择是 pgAdmin,它已经内置了脚本