PGAdmin中的PostgreSQL语法错误 [英] PostgreSQL Syntax error in PGAdmin

查看:138
本文介绍了PGAdmin中的PostgreSQL语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

DECLARE
  num INTEGER;

BEGIN

  num := 3;
  PRINT num;

END;


更新:
好的,让我尝试解释一下.我来自SQL Server背景.在管理工作室中,我可以打开查询窗口并处理(T)-SQL查询.


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.

例如,我可以这样写:

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

我知道这是一个愚蠢的示例,但是我只是在尝试声明一个变量并对其进行处理.我正在尝试使自己熟悉PL/PGSQL.

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.

再次更新:
又是我.我正在尝试下面的脚本,并得到"[ERROR] 7.0-2:语法错误,意外字符".这是要在PGAdmin中工作吗?

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?

DECLARE
  num INTEGER;

BEGIN

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

END;

推荐答案

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

You can use the do statement. For example:

do $$
declare 
  num integer := 10;
begin

    RAISE INFO 'VARIABLE: %', num;

end;
$$language plpgsql;

使用pgadmin时,必须使用按钮EXECUTE QUERY代替 执行pdScript,如此处所述:

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

do语句的文档在这里:

The documentation for do statements is here:

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

这篇关于PGAdmin中的PostgreSQL语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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