2016-11-23 78 views
0

我想在PostgreSQL的SQL脚本声明具有默认值0整数变量:PostgreSQL的 - 声明整型变量

DECLARE user_id integer; 

但它返回例外:

ERROR: syntax error at or near "integer"

我不知道怎么样我可以声明一个变量,然后在while循环中使用这个变量。

+0

http://stackoverflow.com/q/1490942/330315 –

回答

2

您必须将您的代码放入用户定义的函数中。它不会在一个SQL窗口上工作。下面的实施例是,将返回数字你send.the可变

--mybase is your database name. If public, remove it. 
    CREATE OR REPLACE FUNCTION mybase.my_new_rotine(numeric) 
    RETURNS numeric AS 
    $BODY$ 
    --here, get the first variable from function 
    declare id numeric= $1; 

    begin 
    --return the number 
     return id; 
    end; 
    $BODY$ 

    LANGUAGE plpgsql VOLATILE 

的话,就可以用它上的SQL窗口,这样的功能:

select * from mybase.my_new_rotine(1) 

将返回1