将表迁移到Sql XML字段

时间:2012-02-14 18:33:07

标签: xml database sql-server-2008 sql-server-2005 xsd

对于这个长期的问题,我感到非常抱歉!我不知道如何总结它。 案例非常简单,但我是SQL XML的新手。

我有一个表,我想将它的所有记录迁移到另一个带有一个xml列的表。

这是我的字段表

CREATE TABLE [dbo].[Fields] (
[Id]          BIGINT     IDENTITY (1, 1) NOT NULL,
[Title]       NCHAR (10) NOT NULL,
[Duration]    INT        NOT NULL,
[Cost]        MONEY      NOT NULL,
[Consignee]   BIGINT     NOT NULL,
[Date]        DATETIME   NOT NULL,
[TariffId]    BIGINT     NOT NULL,
[InvoiceType] NCHAR (10) NOT NULL,
[IsPayed]     BIT        NOT NULL
);

这个是我的 TypedXML表

CREATE TABLE [dbo].[TypedXml](
    [Id] [bigint] IDENTITY(1,1) NOT NULL,
    [InvoiceItem] [xml](CONTENT [dbo].[invoiceCollection])

其中有一个架构集合,如下所示:

CREATE XML SCHEMA COLLECTION invoiceCollection AS 
'<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
  xmlns="http://www.oliol.com" 
   elementFormDefault="qualified"
  targetNamespace="http://www.oliol.com">
 <xsd:element name="Invoice" type="InvoiceType" />
  <xsd:complexType name="InvoiceType">
    <xsd:sequence>
      <xsd:element name="Id" type="xsd:long" />
      <xsd:element name="Title" type="xsd:string" />
      <xsd:element name="Duration" type="xsd:long" />
      <xsd:element name="Cost" type="xsd:decimal" />
      <xsd:element name="Consignee" type="xsd:long" />
      <xsd:element name="Date" type="xsd:dateTime" />
      <xsd:element name="TariffId" type="xsd:long" />
      <xsd:element name="InvoiceType" type="xsd:string" />
      <xsd:element name="IsPayed" type="xsd:int" />
    </xsd:sequence>
  </xsd:complexType>
</xsd:schema>'

现在,我将其写入迁移

declare @i bigint
set @i=1
while(@i<=10000)
begin

insert into dbo.TypedXML(invoiceitem)
values(
(SELECT  *
FROM Fields
where id=1
FOR XML PATH('Invoice')))
set @i=@i+1
End

它无法插入,因为它尝试插入这样的内容:

插入失败

<Invoice>
  <Id>1</Id>
  <Title>t1</Title>
  <Duration>726643700</Duration>
  <Cost>312118909727165.6133</Cost>
  <Consignee>3120910928797722624</Consignee>
  <Date>4543-07-16T01:40:29.623</Date>
  <TariffId>3120910928797722624</TariffId>
  <InvoiceType>it1</InvoiceType>
  <IsPayed>1</IsPayed>
</Invoice>

虽然我可以像这样插入TypedXML:

Insert Succeed

INSERT typedxml VALUES('
<xml version=1>
<Invoice xmlns="http://www.oliol.com">
  <Id>1</Id>
  <Title>t1</Title>
  <Duration>726643700</Duration>
  <Cost>312118909727165.6133</Cost>
  <Consignee>3120910928797722624</Consignee>
  <Date>4543-07-16T01:40:29.623</Date>
  <TariffId>3120910928797722624</TariffId>
  <InvoiceType>it1</InvoiceType>
  <IsPayed>1</IsPayed>
</Invoice>
')

我想知道如何更改我的迁移查询,以便将 xmlns =“http://www.oliol.com”附加到 Invoice 元素?

P.S: 我改变了这样:     使用XMLNAMESPACES('http://www.oliol.com'作为ns)     选择 *     来自领域     其中id = 1     FOR XML PATH('发票') 但它不符合模式,因为它产生:

<Invoice xmlns:ns="http://www.shaar.com">
  <Id>1</Id>
  <Title>t1</Title>
  <Duration>726643700</Duration>
  <Cost>312118909727165.6133</Cost>
  <Consignee>3120910928797722624</Consignee>
  <Date>4543-07-16T01:40:29.623</Date>
  <TariffId>3120910928797722624</TariffId>
  <InvoiceType>it1</InvoiceType>
  <IsPayed>1</IsPayed>
</Invoice>

1 个答案:

答案 0 :(得分:1)

使用default指定您的命名空间:

;WITH XMLNAMESPACES (default 'http://www.oliol.com')