PostgreSQL:如何检查XML中是否存在节点

时间:2019-11-27 13:25:35

标签: xml postgresql

如何检查XML中是否存在节点。这是我尝试过的功能。

    CREATE OR REPLACE FUNCTION dbo.ab001(
         IN par_params TEXT DEFAULT null)
    RETURNS void
    AS
    $BODY$
    DECLARE
        var__query TEXT DEFAULT '';
    BEGIN
        IF NOT (par_Params IS NULL OR par_Params = '') THEN
                if not xmlexist('//IncludeSettle' passing by ref par_Params)
                    var__query := var__query || ' AND (TDlCd.T_Type_ID <> ''V'')'
                if not xmlexist('//IncludeState' passing by ref par_Params)
                    var__query := var__query || ' AND (TDlCd.T_Type_ID <> ''R'')'
        END IF;
    END;
    $BODY$
    LANGUAGE  plpgsql;

使用此方法,我得到以下错误提示:

 ERROR:  missing "THEN" at end of SQL expression
 LINE 12:    END IF;

然后我添加然后结束,如果又回到错误以下:

    CREATE OR REPLACE FUNCTION dbo.ab001(
         IN par_params TEXT DEFAULT null)
    RETURNS void
    AS
    $BODY$
    DECLARE
        var__query TEXT DEFAULT '';
    BEGIN
        IF NOT (par_Params IS NULL OR par_Params = '') THEN
                if not xmlexist('//IncludeSettle' passing by ref par_Params) then
                    var__query := var__query || ' AND (TDlCd.T_Type_ID <> ''V'')'
                end if;
                if not xmlexist('//IncludeState' passing by ref par_Params) then
                    var__query := var__query || ' AND (TDlCd.T_Type_ID <> ''R'')'
                end if;
        END IF;
    END;
    $BODY$
    LANGUAGE  plpgsql;

错误:

 ERROR:  syntax error at or near "passing"
 LINE 10:    if not xmlexist('//IncludeSettle' passing by r...

但是以下查询可以正常工作。

 SELECT xmlexists('//IncludeSettle' PASSING BY REF '<dummy_root><IncludeSettle/><StartTime/></dummy_root>');

结果为真。

我该怎么做。任何帮助都非常感谢。

这里是链接: https://rextester.com/WSCY48060

1 个答案:

答案 0 :(得分:1)

它是xmlexists而不是xmlexist

此作品有效:

   CREATE OR REPLACE FUNCTION ab001(
         IN par_params TEXT DEFAULT null)
    RETURNS void
    AS
    $BODY$
    DECLARE
        var__query TEXT DEFAULT '';
    BEGIN
        IF NOT (par_Params IS NULL OR par_Params = '') THEN
                if not xmlexists('//IncludeSettle' passing by ref par_Params) then
                    var__query := var__query || ' AND (TDlCd.T_Type_ID <> ''V'')';
                end if;
                if not xmlexists('//IncludeState' passing by ref par_Params) then
                    var__query := var__query || ' AND (TDlCd.T_Type_ID <> ''R'')';
                end if;
        END IF;
    END;
    $BODY$
    LANGUAGE  plpgsql;