我是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:
postgresql.1045698.n5.nabble/PgAmin3-Anonymous-code-block-can-t-by-pressed by-pressing-quot-Execute-PG-script -quot-button-td5771073.html
do语句的文档在这里:
The documentation for do statements is here:
www.postgresql/docs/9.3/static/sql-do .html
更多推荐
PGAdmin中的PostgreSQL语法错误
发布评论