简体   繁体   中英

XML query in SQL Server 2005

a query i am running is not functioning properly, here is my code:

select  a.id, a.userid, c.firstname + ' ' + c.lastname AS Name, a.objectid, a.settings,
CAST(settings as XML).exist('property[@name=''colFirstChoiceVendorPaymentTerms'']') as first,
CAST(settings as XML).exist('property[@name="colSecondChoiceVendorPaymentTerms"]//property[@name=''Visible'' and text()=''true'']') as second,
CAST(settings as XML).exist('property[@name="colThirdChoiceVendorPaymentTerms"]//property[@name=''Visible'' and text()=''true'']') as third
FROM wcukopera05.vstx.dbo.screenlayout a
join    wcuksql01.hrsystem.dbo.person c 
on      a.UserId=c.Id
where   a.objectid = 'gridViewCustomerCurrentRatesCosts'

I have a lengthy piece of XML and i want to check if certain properties such as(colThirdChoiceVendorPaymentTerms) are present in the XML AND to see if the visibility is true.

At the minute my code returns all the correct columns but the values for columns 'first', 'second' and 'third' all return 0. But some of them should be returning 1...

I dont understand why all of they all return 0?

I have uploaded the XML here: txt.do/dev1 you will see that the visibility for colFirstChoiceVendorPaymentTerms and colSecondChoiceVendorPaymentTerms is set to true. but there is no visibility for colThirdChoiceVendorPaymentTerms. this is what i want to check. if visibility and column are there or not.

Thank you again for your help.

'property[@name="colSecondChoiceVendorPaymentTerms"]//property[@name=''Visible'' and text()=''true'']'

I dont understand why all of they all return 0?

  1. You are looking for a property root node.
  2. You are comparing the attribute @name where you should compare against a node value
  3. You are one property level too deep when you start to look for the property with name Visible.

Your should look like this.

'//property[.="colSecondChoiceVendorPaymentTerms"]/../property[@name=''Visible'' and text()=''true'']'

Or you could not use deep search and use value to get the value of the node @name="Visible" .

settings.value('(/XtraSerializer/property/property[property/@name="Name" and property = "colSecondChoiceVendorPaymentTerms"]/property[@name = "Visible"])[1]', 'bit')

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