2
我对这个长的问题非常遗憾!我不知道如何总结它。 的情况下是很简单,但我是新来的SQL XML。迁移表到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 @[email protected]+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”到发票元素?
PS: 我改变这样的: WITH XMLNAMESPACES( 'http://www.oliol.com' 为纳秒) SELECT * FROM 领域 其中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>
谢谢Mikael!那太棒了!这种名称间距是否正确? – 2012-02-15 08:04:32
@Reza它是产生在SQL Server你想要的XML的正确方法。我不知道这是否是在像这样的模式中使用名称空间的最佳方式。我没有那么多的经验。 – 2012-02-15 08:21:42
这就是我的问题。在像这样的模式中使用名称空间的最佳方式是什么? – 2012-02-15 16:34:27