XML QUERY LANGUAGES: XQUERY AND XPATH The xml.value

XML QUERY LANGUAGES: XQUERY AND XPATH The xml.value function must return a single scalar value, or else an error is thrown. You wouldn t need to have xml.value to return a single XMLdata type, because that s what xml.querydoes. Using our overly simple document from the xml.existsection: CREATE TABLE xml_tab( the_id INTEGER PRIMARY KEY IDENTITY, xml_col XML) GO INSERT xml_tab VALUES( ) INSERT xml_tab VALUES( ) INSERT xml_tab VALUES( ) GO the following SQL query using xml.value: SELECT the_id, xml_col.value( /people[1]/person[1]/@name , varchar(50) ) AS name FROM xml_tab would produce a single row containing the_idand namecolumns for each row. The result would look like this: the_id name – 1 curly 2 larry 3 moe This function must only return a scalar value, however, so if the following row was added to the table: INSERT xml_tab VALUES( ) GO the same SQL query would only use the first person, even though there are two person elements. Note that we ve had to use the subscript [1] in every query to ensure that we re only getting the first person. This is because of SQL Server XQuery s static typing. If we didn t use the subscript, the engine would have to assume that the document permits multiple person elements, and would fail with the error XQuery: Expression must return a singleton.

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Tomcat Web Hosting services

Bookmark the permalink.

Comments are closed.