无法在postgresql中运行函数

时间:2013-01-07 09:37:40

标签: postgresql

我是postgresql的新用户,第一次我想在postgresql中编写一个函数。我从pgsql复制并粘贴了一个示例,但发生了错误。 这是我的代码:

CREATE FUNCTION somefunc() RETURNS integer AS $$
<< outerblock >>
DECLARE
    quantity integer := 30;
BEGIN
    RAISE NOTICE 'Quantity here is %', quantity;  -- Prints 30
    quantity := 50;
    --
    -- Create a subblock
    --
    DECLARE
        quantity integer := 80;
    BEGIN
        RAISE NOTICE 'Quantity here is %', quantity;  -- Prints 80
        RAISE NOTICE 'Outer quantity here is %', outerblock.quantity;  -- Prints 50
    END;

    RAISE NOTICE 'Quantity here is %', quantity;  -- Prints 50

    RETURN quantity;
END;
$$ LANGUAGE plpgsql;

和我的错误:

An error has occured
Fehler:Syntaxfehler bei "CREATE"
Line2:$Body$CREATE FUNCTION somefunc () RETURNS integer AS $$

修改 我在代码部分编写了上面的代码,但psql在SQL部分生成了相同的代码,并附加了一行:

$BODY$CREATE FUNCTION somefunc() RETURNS integer AS $$

并且此行中出现错误 enter image description here 如果我删除此行,则在下一行中发生此错误: enter image description here

1 个答案:

答案 0 :(得分:0)

我的猜测是你正在使用GUI来生成你的功能。如果是这种情况,则GUI可能包含函数名称和参数等字段,您填写(或忽略)。因此,您现在输入的代码将是函数的正文区域,GUI已经提示您输入所有样板信息。如果所有这些都是这种情况,那么您应该只在该字段中包含函数体代码,而不是完整的函数创建代码(因为应用程序将根据您输入GUI的内容为您生成)。

或者,如果应用程序允许您执行任意SQL(确实如此),则可以将CREATE作为任意SQL执行,而不是使用GUI。