Udostępnij za pośrednictwem


Funkcja PRAWDA (XQuery)

Zwraca wartość True typu xs:boolean.Jest to odpowiednik funkcji xs:boolean("1").

Składnia

fn:true() as xs:boolean

Przykłady

This topic provides XQuery examples against XML instances that are stored in various xml type columns in the AdventureWorks database.For an overview of each of these columns, see xml Data Type Representation in the AdventureWorks Database.

A.W tym artykule opisano prologu XQuery.

The following example queries an untyped xml variable.The expression in the value() method returns Boolean true() if "aaa" is the attribute value.The value() method of the xml data type converts the Boolean value into a bit and returns it.

DECLARE @x XML
SET @x= '<ROOT><elem attr="aaa">bbb</elem></ROOT>'
select @x.value(' if ( (/ROOT/elem/@attr)[1] eq "aaa" ) then fn:true() else fn:false() ', 'bit')
go
-- result = 1

In the following example, the query is specified against a typed xml column.The if expression checks the typed Boolean value of the <ROOT> element and returns the constructed XML, accordingly.Jeśli jest mieszaniną tego typu lub inne wartości z innych typów są przekazywane, błąd statycznego zostanie zaokrąglona.

  • Tworzy kolekcję schematów XML, która definiuje element <ROOT> typu xs:boolean.

  • Creates a table with a typed xml column by using the XML schema collection.

  • Zapisuje wystąpienie XML w kolumnie i wykonuje na nim kwerendę.

-- Drop table if exist
--DROP TABLE T
--go
DROP XML SCHEMA COLLECTION SC
go
CREATE XML SCHEMA COLLECTION SC AS '
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="QNameXSD" >
      <element name="ROOT" type="boolean" nillable="true"/>
</schema>'
go
CREATE TABLE T (xmlCol XML(SC))
go
-- following OK
insert into T values ('<ROOT xmlns="QNameXSD">true</ROOT>')
 go
-- Retrieve the local name. 
SELECT xmlCol.query('declare namespace a="QNameXSD"; 
   if (/a:ROOT[1] eq true()) then
       <result>Found boolean true</result>
   else
       <result>Found boolean false</result>')

FROM T
-- result = <result>Found boolean true</result>
-- Clean up
DROP TABLE T
go
DROP XML SCHEMA COLLECTION SC
go