喜欢:在 postgresql 中以变量值结尾的通配符

Like : Wildcard ending with variable value in pgsql

我已经这样编写了我的 pgsql 块,我从该块的上部接收 var 的值:

    DO $$
declare
var integer ;
b integer;
k integer;
p integer;
m integer;
n varchar(100);
begin

k := null;
    var := (select
   count(child.relname) AS child

FROM pg_inherits
    JOIN pg_class parent            ON pg_inherits.inhparent = parent.oid
JOIN pg_class child             ON pg_inherits.inhrelid   = child.oid
JOIN pg_namespace nmsp_parent   ON nmsp_parent.oid  = parent.relnamespace
JOIN pg_namespace nmsp_child    ON nmsp_child.oid   = child.relnamespace
WHERE parent.relname='abc');


 while k = null loop

k := (select
   child.relname AS child

FROM pg_inherits
JOIN pg_class parent            ON pg_inherits.inhparent = parent.oid
JOIN pg_class child             ON pg_inherits.inhrelid   = child.oid
JOIN pg_namespace nmsp_parent   ON nmsp_parent.oid  = parent.relnamespace
JOIN pg_namespace nmsp_child    ON nmsp_child.oid   = child.relnamespace
   WHERE parent.relname='abc' and child.relname like '%',var);

  var := var + 1;

   end loop;

这不是完整的块。但是,从块中你可以了解到我需要找出以变量名结尾的分区 table(value) 。但是,当我 运行 这个时,我得到以下错误:

 ERROR:  syntax error at or near ","
LINE 34: ...RE parent.relname='abc' and child.relname like '%',var);

我尝试使用双 %% 符号(一个用于通配符,另一个用于获取变量值,然后出现以下错误:

    ERROR:  syntax error at or near ","
 LINE 34: ...E parent.relname='abc' and child.relname like '%%',var);

任何人都可以让我知道是否有任何其他方法可以使用通配符访问 pgsql 变量值。

您需要使用字符串连接:

... child.relname LIKE ('%' || var)