简体   繁体   中英

How to use Prepared Statement for Microsoft SQL Xpath queries in java

I am having a hard time trying to use Prepared Statement for the below sql xml xpath query in java .

SELECT * 
FROM dbo.EMPLOYEE_DTLS 
WHERE EMPLOYEE_DTLS.EMP_XML.exist('//emp/empId[text()[1]=?]')= 1 AND 
(EMPLOYEE_DTLS.EMP_XML.exist('//emp/designationList/designation[contains(.,?)]')= 1 OR
  EMPLOYEE_DTLS.EMP_XML.exist('//emp/designationList/designation[contains(.,?)]')= 1 OR  1!=1)

I would like to set values to all places with ? symbol. Since the ? is inside the single quotes in xpath, it is not considered as a placeholder and I am unable to set the values. Can someone help me on this. Any help is greatly appreciated.

Directly specifying a parametrized value in an XPath (or XQuery) expression is not possible. What you can do however, is refer to a variable using the sql:variable("variable_name") function in XPath (XQuery) expressions.

So you'd have a script like:

DECLARE @some_variable NVARCHAR(128)=?; -- specify the parameter here
SELECT * 
FROM dbo.EMPLOYEE_DTLS 
WHERE EMPLOYEE_DTLS.EMP_XML.exist('//emp/empId[text()[1]=sql:variable("@some_variable")]')=1 -- etc.

You can find documentation on the sql:variable() function → here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM