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