对于这么长的问题,我感到非常抱歉!我不知道如何总结它。 这个案例非常简单,但我是 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>'
现在,我将此写入迁移:
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>
最佳答案
使用 default
指定您的命名空间:
;WITH XMLNAMESPACES (default 'http://www.oliol.com')
关于xml - 将表迁移到 Sql XML 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9282263/