0

我有一个奇怪的问题。在数据库中,我安装了 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.

我必须做些什么来计算行数?

4

2 回答 2

1

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

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

于 2013-09-06T07:18:17.200 回答
1

问题在这里:

DECLARE count integer; 

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

于 2013-09-05T14:30:54.050 回答