如何修复plpgsql LIKE条件非整数类型未找到?

时间:2019-04-08 18:45:09

标签: sql postgresql

我已经在PostgreSQL中创建了用于循环的函数,当该函数在那儿运行时首先检查运行情况。此功能是否仍在使用相同参数运行?如果否,则结束。但是,如果参数不是整数值,我会发现LIKE条件存在问题。

我已经测试了整数值:

CREATE OR REPLACE FUNCTION public.looping(sys_date timestamp, n integer, meber_code varchar)
  RETURNS character varying AS
$BODY$ 
DECLARE
  checkFunctionRunning integer := 0;
  queryRunning text;
  concatText text := '%LOOPING%'||$2||'%';
BEGIN
 RAISE NOTICE 'Concat: %', concatText;
 SELECT COUNT(1) INTO checkFunctionRunning 
 FROM pg_stat_activity 
 WHERE 1=1
 AND UPPER(state)='ACTIVE' 
 --AND UPPER(query) LIKE '%LOOPING(timestamp '||$1||',%'
 AND UPPER(query) LIKE concatText
 AND UPPER(query) NOT LIKE '%PG_STAT%'
 ;
 IF checkFunctionRunning = 1 THEN
 FOR counter IN 1..n LOOP
 RAISE NOTICE 'Counter: %, Nilai N : %, Param 3 : %', counter,n,$3;
 PERFORM pg_sleep(10);
   END LOOP;
 return n;
 ELSE
    --return '%select looping('||$1||');% '||$1||'-'||checkFunctionRunning;
    SELECT query||' - this is queryRunning '||checkFunctionRunning into queryRunning
    FROM pg_stat_activity 
    WHERE 1=1
    AND UPPER(state)='ACTIVE' 
    AND UPPER(query) LIKE concatText
    AND UPPER(query) NOT LIKE '%PG_STAT%'
    ;
    return queryRunning;
    --return 'Currently there is function running with same parameters';
 END IF;
END ; 
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION public.looping(timestamp,integer,varchar)
  OWNER TO postgres;

然后我在下面使用此代码运行:

select looping(now()::timestamp,10,'Param3');

该功能正常运行。 结果:

NOTICE:  Concat: %LOOPING%10%
CONTEXT:  PL/pgSQL function looping(timestamp without time zone,integer,character varying) line 7 at RAISE
NOTICE:  Counter: 1, Nilai N : 10, Param 3 : Param3
CONTEXT:  PL/pgSQL function looping(timestamp without time zone,integer,character varying) line 18 at RAISE
NOTICE:  Counter: 2, Nilai N : 10, Param 3 : Param3
CONTEXT:  PL/pgSQL function looping(timestamp without time zone,integer,character varying) line 18 at RAISE
NOTICE:  Counter: 3, Nilai N : 10, Param 3 : Param3
CONTEXT:  PL/pgSQL function looping(timestamp without time zone,integer,character varying) line 18 at RAISE
NOTICE:  Counter: 4, Nilai N : 10, Param 3 : Param3
CONTEXT:  PL/pgSQL function looping(timestamp without time zone,integer,character varying) line 18 at RAISE
NOTICE:  Counter: 5, Nilai N : 10, Param 3 : Param3
CONTEXT:  PL/pgSQL function looping(timestamp without time zone,integer,character varying) line 18 at RAISE
NOTICE:  Counter: 6, Nilai N : 10, Param 3 : Param3
CONTEXT:  PL/pgSQL function looping(timestamp without time zone,integer,character varying) line 18 at RAISE
NOTICE:  Counter: 7, Nilai N : 10, Param 3 : Param3
CONTEXT:  PL/pgSQL function looping(timestamp without time zone,integer,character varying) line 18 at RAISE
NOTICE:  Counter: 8, Nilai N : 10, Param 3 : Param3
CONTEXT:  PL/pgSQL function looping(timestamp without time zone,integer,character varying) line 18 at RAISE
NOTICE:  Counter: 9, Nilai N : 10, Param 3 : Param3
CONTEXT:  PL/pgSQL function looping(timestamp without time zone,integer,character varying) line 18 at RAISE
NOTICE:  Counter: 10, Nilai N : 10, Param 3 : Param3
CONTEXT:  PL/pgSQL function looping(timestamp without time zone,integer,character varying) line 18 at RAISE
Total query runtime: 10.0 secs
1 row retrieved.

然后我改变了

concatText text := '%LOOPING%'||$2||'%';

进入

concatText text := '%LOOPING%'||$2||','||quote_literal($3)||'%';

然后我得到结果:

NOTICE:  Concat: %LOOPING%10,'Param3'%
CONTEXT:  PL/pgSQL function looping(timestamp without time zone,integer,character varying) line 7 at RAISE

Total query runtime: 13 msec
1 row retrieved.
"Currently there is function running with same parameters - 0"

我的concatText for LIKE条件是否错误?

关于, 学生

0 个答案:

没有答案