PostgreSQL error:recursive Query "T" column 2 has type character varying ($) in non-recursive term but type character VA Rying overall

Source: Internet
Author: User
Tags postgresql



Recently, when doing a project, there is a need to find out the current logged in the user's jurisdiction over all areas of the data, and the query of the Department information in the following format to show



Supreme People's Court > Jiangsu Provincial Higher courts > Nantong Intermediate Court
Supreme People's Court > Jiangsu Provincial Higher courts > Lianyungang Intermediate Court



, then query with the following statement


 
 
WITH RECURSIVE T AS (
    SELECT
        c_id,
        c_name
    FROM
        db_aty.t_aty_corp
    WHERE
        c_pid IS NULL
    UNION ALL
        SELECT
            D.c_id,
            T.c_name || ‘>‘ || D.c_name
        FROM
            db_aty.t_aty_corp D
        JOIN T ON D.c_pid = T .c_id
) SELECT
    c_id AS corpId,
    c_name AS corpName
FROM
    T


However, the following error occurred



error:recursive query "T" column 2 has type character varying ($) in non-recursive term but type character varying over All






Guess this is because the use of union ALL in an SQL statement requires the same field properties that are queried before and after the query, using T.c_name | | ' > ' | | D.c_name is a concatenation of strings, so the field properties are associated with the previous



C_name inconsistent, so cause error,



Workaround: Specify the field format for the stitched string


 
 
WITH RECURSIVE T  AS (
    SELECT
        c_id,
        c_name::varchar(150)
    
    FROM
        db_aty.t_aty_corp
    WHERE
        c_pid is null
    UNION ALL
        SELECT
            D.c_id,
            (T.c_name   || ‘>‘ || D.c_name )::varchar(150) as c_name
        FROM
            db_aty.t_aty_corp D
        JOIN T ON D.c_pid = T .c_id
) 
SELECT c_id AS corpId ,c_name as corpName  FROM T


Get the correct answer as follows









PostgreSQL error:recursive Query "T" column 2 has type character varying ($) in non-recursive term but type character VA Rying overall


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.