function count()是PL / Pgsql中的变量

时间:2013-09-05 14:18:02

标签: sql postgresql count plpgsql

我遇到了一个奇怪的问题。在数据库中,我安装了plpgsql语言,但是在这段代码中:

CREATE OR REPLACE FUNCTION add_sala() RETURNS void AS
$BODY$
DECLARE i integer;
DECLARE IdConfAux integer;
DECLARE count integer; 
DECLARE a integer;

BEGIN   
    LOCK TABLE "TabelaSalas" IN ACCESS EXCLUSIVE MODE NOWAIT;

    UPDATE "TabelaSalas" SET "NumJogadoresAtual" = 0, "EstadoSala"= 0,"CronometroIniciado"=0, "TempoInicioJogo"=15
    WHERE "NumJogadoresAtual"<0;

    CREATE TEMP TABLE tempIds ON COMMIT DROP AS 
    SELECT "IdConf" FROM "ConfiguracaoSalas" WHERE "Senha" IS NULL;

    SELECT COUNT(*) INTO count FROM tempIds;
    RAISE NOTICE 'count = % ',count;

    WHILE count > 0 LOOP
        SELECT "IdConf" INTO IdConfAux FROM tempIds LIMIT 1;

        SELECT COUNT(*) INTO i FROM "TabelaSalas"
        WHERE IdConfAux = "IdConf" 
        AND "NumJogadoresAtual" = 0 ;
        RAISE NOTICE 'i= % IDCONF = %', i,IdConfAux;
            IF i=0 THEN
                RAISE NOTICE 'entrou %',IdConfAux;
                INSERT INTO "TabelaSalas"("IdConf","DataCriacao","OrdemQuestoes","NumJogadoresAtual","TempoInicioJogo","CronometroIniciado","EstadoSala")
                SELECT  IdConfAux, NOW(),"OrdemQuestoes",0,15,0,0 FROM "TabelaSalas" LIMIT 1 ;
            END IF;
        DELETE FROM tempIds WHERE "IdConf" IN (SELECT * FROM tempIds LIMIT 1);
        i=1;
        count = count - 1;      
    END LOOP;


END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;

我有这个错误。看起来COUNT被认为是函数的变量:

ERRO:  erro de sintaxe em ou próximo a "("
LINE 1: SELECT  $1 () FROM tempIds
                   ^
QUERY:  SELECT  $1 () FROM tempIds
CONTEXT:  SQL statement in PL/PgSQL function "add_sala" near line 15.

我有什么计算行?

2 个答案:

答案 0 :(得分:1)

这是SQL和PL / pgSQL标识符的冲突。请升级。现代版本使用一种更简单的算法将占位符放置到嵌入式SQL中,主要是,当识别出碰撞时,可以引发更详细和可读的异常。

在这种情况下,你是一个快乐的人。可能存在奇怪的不可见和不需要的行为 - 因此语法错误更好。

答案 1 :(得分:0)

问题在于:

DECLARE count integer; 

您将count声明为函数变量。将变量声明更改为其他内容。与cntcount_of_something

相同