为什么变量被视为列?

时间:2011-11-11 02:50:34

标签: sql postgresql plpgsql

我要创建此函数,但变量view_size被视为列。

我收到错误消息column view_size don't exists.

为什么?

如果我改变了上下文,它就可以了(例如:只进行查询,被视为变量)。

CREATE OR REPLACE FUNCTION create_view(view_size INTEGER) RETURNS VOID  
AS $$
BEGIN
    CREATE VIEW token_relation
    AS
    SELECT * FROM textblockhastoken     
    ORDER BY textblockid, sentence, position
    LIMIT view_size;
END
$$
LANGUAGE plpgsql;

1 个答案:

答案 0 :(得分:2)

请改为尝试:

CREATE OR REPLACE FUNCTION create_view(view_size INTEGER) RETURNS VOID  
AS $$
BEGIN
    EXECUTE 'CREATE VIEW token_relation 
    AS 
    SELECT * FROM textblockhastoken 
    ORDER BY textblockid, sentence, position 
    LIMIT ' || view_size;
END
$$
LANGUAGE plpgsql;