代码之家  ›  专栏  ›  技术社区  ›  MB34

postgres函数传递字符串数组

  •  0
  • MB34  · 技术社区  · 6 年前

    我有下面的postgres函数从数据库返回一些信息。我需要pUic参数能够 串。

    CREATE OR REPLACE FUNCTION eddie.getinv(
        IN p_ic character varying[],
        IN p_id character varying)
      RETURNS TABLE(cnt bigint, actualid text, actualcompany text, part text, daysinstock double precision, condition text, 
                    ic text, price numeric, stock text, quantity bigint, location text, comments text) AS
    $
    BEGIN
        RETURN QUERY
            WITH cte AS (
                    SELECT 
                    CASE WHEN partnerslist IS NULL OR partnerslist = '' THEN
                        'XX99'
                    ELSE 
                        partnerslist
                    END AS a
                FROM support.members WHERE id = p_id
            ), ctegroup AS
            (    
                SELECT 
                   u.id AS actualid,
                  (SELECT m.company || ' (' || m.id ||')' FROM support.members m WHERE m.id = u.id) AS actualcompany,
                  u.itemname AS part, 
                  DATE_PART('day', CURRENT_TIMESTAMP - u.datein::timestamp) AS daysinstock, 
                  TRIM(u.grade)::character varying AS condition, 
                  u.vstockno::text AS stock, 
                  u.holl::text AS ic, 
                  CASE WHEN u.rprice > 0 THEN 
                        u.rprice 
                  ELSE 
                        NULL 
                  END AS price, 
                  u.quantity, 
                  u.location,
                  u.comments::text
                FROM public.net u 
                WHERE u.holl in (p_ic)
                  AND visibledate <= now() 
                  AND u.id = ANY(REGEXP_SPLIT_TO_ARRAY(p_id ||','|| (SELECT a FROM cte), ','))
                ORDER BY u.itemname, u.id
            )  
            SELECT 
                COUNT(ctegroup.ic) OVER(PARTITION BY ctegroup.ic ORDER BY ctegroup.ic) AS cnt,
                actualid,
                MAX(actualcompany) AS actualcompany,
                MAX(part) AS part,
                MAX(daysinstock) AS daysinstock, 
                STRING_AGG(condition,',') AS condition, 
                MAX(ic) AS ic,
                MAX(price) AS price,
                STRING_AGG(stock,',') AS stock, 
                SUM(quantity) AS qty, 
                STRING_AGG(location,',') AS location,
                STRING_AGG(comments,';') AS comments
            FROM ctegroup
            GROUP BY part, actualid, ic
            ORDER BY actualid;
    END; $
    
    LANGUAGE 'plpgsql';
    

    我在pgadminiii i查询窗口中调用它,如下所示:

    SELECT * FROM eddie.getinv(array['536-01036','536-01033L','536-01037'], 'N40')
    

    但它返回了这个错误:

    ERROR:  operator does not exist: text = character varying[]`
    LINE 28:             WHERE u.holl in (p_ic)`
    

    我该怎么解决这个问题,还是我打错电话了?我将从类似于以下的php api函数调用它:

    $id = 'N40';
    $ic = array('536-01036','536-01033L','536-01037');
    
    $sql = "SELECT * FROM eddie.getinv(array['". implode("','",$ic)."'], '".$id."');";
    
    try 
    {       
        $results = pg_query($sql);
        if(pg_num_rows($results) == 0) {
            $rows = [];
        }
        else
        {
            $data = pg_fetch_all($results);
            foreach($data as $item)
            {                    
                $rows[$item["ic"]][] = $item;
            }
        }
        pg_free_result($results);
    }
    catch (Exception $e) 
    {
        $err = array("message"=>$e->getMessage(), "code"=> $e->getCode(), "error"=>$e->__toString().",\n".print_r($_REQUEST, true));
        echo json_encode($err);
    }
    echo json_encode($rows);
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Nick Barnes    6 年前

    看起来你的数组被传递给了函数。问题出在你的查询中。

    IN () 子句需要以逗号分隔的值列表。当您在其中放置一个数组时,它被解释为一个元素列表,其中的值是整个数组。换句话说, u.holl in (p_ic) 将检查是否 u.holl 等于 p_ic ,并且由于类型不匹配,比较失败。

    如果要根据 内容 在数组中,使用 u.holl = ANY(p_ic) .