English 中文(简体)
采用TSQL
原标题:Parse / query XML using TSQL
  • 时间:2012-01-12 23:10:43
  •  标签:
  • sql
  • xml
  • t-sql

当时我一直在打仗,似乎我很接近,但情况并非如此。 我在数据库中有一个一栏。

<document>
<items>
<item name="one">one is the first number</item>
<item name="two">two is the second number</item>
</items>
</document>

在这种例子中,我需要问询和回来两个是第二位。 我也希望这样做,不要制造一个温床。 目前,我有:

create table #test (item1 xml)
insert into #test (item1) 
values ( <document> <items> <item name="one">one is the first number</item> <item name="two">two is the second number</item> </items> </document> )

select item1.value( (/document/items/item)[2] ,  nvarchar(max) ) from #test
select item1.query( /document/items/item[@name="two"] ) from #test

The first select returns the correct value but I need to know that it s the 2nd index The second returns what I want but it returns the entire node two..

我失踪了什么? 而且,在不转换为温带的情况下使用XML是否简单?

最佳回答

我也希望这样做,不要制造一个温床。

你可以使用一种数据类型XML的变量。

declare @xml xml

set @xml = 
 <document>
  <items>
    <item name="one">one is the first number</item>
    <item name="two">two is the second number</item>
  </items>
</document> 

select @xml.value( (/document/items/item[@name="two"])[1] ,  nvarchar(max) )

或你可以在问询中把你的扼杀引向XML。

select cast(
             <document>
              <items>
                <item name="one">one is the first number</item>
                <item name="two">two is the second number</item>
              </items>
            </document>  as xml
           ).value( (/document/items/item[@name="two"])[1] ,  nvarchar(max) )

您的第一问询使用<代码>. 价值()正确,第二问:正确表述。 在使用<代码>价值()时,你需要使用一种能够回归单一价值的XQuery表述。 这将给您所有项目节点:@name 为2/document/items/item[@name=2”]

问题回答

(如我下面所述,第一,而不是一个温带,你可以使用一种类型的<代码>xml。) 这些变量可以直接从直面字面上分配。

因此,我认为你指的是希望<条码>项目与<条码> 姓名<>> 代码>2<>>>>>的文字价值,在这种情况下,你只需要将你使用的xpath的适当条件列入<条码>价值()

DECLARE @x xml

SET @x =  <document> <items> <item name="one">one is the first number</item> 
     <item name="two">two is the second number</item> </items> </document> 

SELECT @x.value( (/document/items/item[@name="two"])[1] ,  nvarchar(max) )

专 员

--------------------------------------------------------------
two is the second number

(1 row(s) affected)




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签