【问题标题】:Migrate Table to Sql XML field将表迁移到 Sql XML 字段
【发布时间】:2012-02-15 04:42:11
【问题描述】:

对于这个冗长的问题,我感到非常抱歉!我不知道怎么概括。 案例很简单,但我是 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])

其中有一个像这样的 schema 集合:

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>'

现在,我写这个来migrate

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 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 元素?

附言: 我已经改变了它: 使用 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>

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢米凯尔!那很棒!这种命名空间正确吗?
    • @Reza 是在 SQL Server 中生成你想要的 XML 的正确方法。我不知道这是否是在这样的模式中使用命名空间的最佳方式。我对此没有太多经验。
    • 这正是我的问题。在这样的模式中使用命名空间的最佳方式是什么?
    • @Reza 我认为您应该将其添加为仅使用 XML 和 XML-SCHEMA 标记的新问题。
    猜你喜欢
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-01
    • 2022-06-14
    • 1970-01-01
    • 2021-09-04
    • 2020-03-13
    相关资源
    最近更新 更多