0

How to check if a node exists in the XML. Here is the function which I tried.

    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;

Using this I am getting below error saying:

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

Then I added then and end if and got back below error:

    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:

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

But the following query works as expected.

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

Result is true.

How can I accomplish this. Any help is really appreciated.

Here is the link: https://rextester.com/WSCY48060

1 Answer 1

1

It is xmlexists and not xmlexist!

This one works:

   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;
Sign up to request clarification or add additional context in comments.

1 Comment

Oh my bad... I overlooked. Thank you. I will try again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.