English 中文(简体)
我如何使用xquery区分微米变量?
原标题:How do I split an xml variable using xquery?

I have a scalar xml variable:

DECLARED @XML xml =
 <rows>
    <row>
        <column1 attrib="" />
        <column2 attrib="" />
    </row>
    <!-- ... -->
</rows> ;

我愿将数据分开,以便每个浏览量×ml在表格中被分配到新的记录:

Id | ... | XML
1  |     |  <row><column1 attrib="" /><column2 attrib="" /></row> 
2  |     | etc.
3  |     | etc.

我很 gr熟,因此我很麻烦地撰写了我想要的话。

INSERT into MyTable( [XML])
SELECT @XML.query( row )

现在,我知道,但看起来不是做我打算做的事,而是插入了多个新记录,在<条码>[XML]栏中插入一个带有空洞的单一记录。

What am I not getting?

Clarification

我不想利用<条码>价值(......)从各行各处获得内文本、子类或属性。 我试图抓住整个<条码>和条形;要素,将其保留到“条码”栏。

I. 进行了nodes(......)的试验,并做了以下试验:

INSERT into MyTable([XML])
SELECT C.query( * )
FROM @XML.nodes( rows/row ) T(C)

这与我所希望的更接近,但结果t并不包括外部<代码><row>tag, 仅限<column*>.

最佳回答

T-SQL :

SET ANSI_WARNINGS ON;

DECLARE @x XML =
 <rows>
    <row Atr1="11" />
    <row Atr1="22" Atr2="B" />
    <row Atr1="33" Atr2="C" />
</rows> ;

DECLARE @Table TABLE(Id INT NOT NULL, [XMLColumn] XML NOT NULL);

INSERT  @Table (Id, XMLColumn)
SELECT  ROW_NUMBER() OVER(ORDER BY @@SPID) AS Id,
        a.b.query( . ) AS [XML]
FROM    @x.nodes( //rows/row ) AS a(b);

SELECT  *
FROM    @Table t;

成果:

Id XMLColumn
-- --------------------------
1  <row Atr1="11" />
2  <row Atr1="22" Atr2="B" />
3  <row Atr1="33" Atr2="C" />
问题回答




相关问题
Export tables from SQL Server to be imported to Oracle 10g

I m trying to export some tables from SQL Server 2005 and then create those tables and populate them in Oracle. I have about 10 tables, varying from 4 columns up to 25. I m not using any constraints/...

SQL server: Can NT accounts be mapped to SQL server accounts

In our database we have an SQL server account that has the correct roles to access some of the databases. We are now switching to windows authentication and I was wondering if we can create a NT user ...

SQL Server 2000, ADO 2.8, VB6

How to determine if a Transaction is active i.e. before issuing Begin Transaction I want to ensure that no previous transaction are open.. the platform is VB6, MS-SQL Server 2000 and ADO 2.8