0
我使用SQL的sp_xml_preparedocument
功能將XML字符串轉換成一個臨時表所示:默認ID
create table #ExportTemplate
(
ExportTemplateId uniqueidentifier not null default newid()
, ExportTypeId uniqueidentifier not null
, TemplateDesc varchar(100) not null
, Active bit not null
, DateAdded datetime not null
, DateModified datetime not null
)
-- If Data is coming in by XML then merge changes into the table
if len(isnull(@XMLString,'')) > 0
begin
print '*** Merging XML Changes'
declare @XMLTable xml
, @idoc int
, @Cnt int
--Load XML
begin
set @XMLTable = @XmlString; --Convert string to xml
exec sp_xml_preparedocument @idoc OUTPUT, @XMLTable
-- insert into #ExportTemplate
select
isnull(cast(et.ExportTemplateId as uniqueidentifier), newid()) as ExportTemplateId,
cast(et.ExportTypeId as uniqueidentifier) as ExportTypeId,
et.TemplateDesc,
et.Active,
getdate() as DateAdded,
getdate() as DateModified
from openxml(@idoc,'/ArrayOfTemplate/Template', 2)
with (
ExportTemplateId uniqueidentifier 'ExportTemplateId',
ExportTypeId uniqueidentifier 'ExportTypeId',
TemplateDesc varchar(100) 'TemplateDesc',
Active bit 'Active'
) et
set @Cnt = @@rowcount;
print '*** ' + convert(varchar,@Cnt) + ' rows were retrieved from the XML Table';
exec sp_xml_removedocument @idoc
end
select * from #ExportTemplate
end
,我通過這個功能的XML看起來是這樣的:
<ArrayOfTemplate xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Template>
<ExportTemplateId>7f0db0ed-d215-4a25-a38a-0305dddc56b3</ExportTemplateId>
<ExportTypeId>408b6a56-3b7c-41d8-902a-a20f5b2df170</ExportTypeId>
<TemplateDesc>Census Standard HR</TemplateDesc>
<Active>false</Active>
<DataElements />
</Template>
<Template>
<ExportTemplateId>13691df1-9b4c-4b94-b106-41494fe172aa</ExportTemplateId>
<ExportTypeId>94fb5330-9bd8-4622-8c26-dd212832b20c</ExportTypeId>
<TemplateDesc>Census Standard Payroll</TemplateDesc>
<Active>true</Active>
<DataElements />
</Template>
<Template>
<ExportTemplateId></ExportTemplateId>
<ExportTypeId>94fb5330-9bd8-4622-8c26-dd212832b20c</ExportTypeId>
<TemplateDesc>Census Standard Payroll New</TemplateDesc>
<Active>true</Active>
<DataElements />
</Template>
</ArrayOfTemplate>
如果我創建一個新的Template
(XML示例中的最後一個模板條目),我希望合併功能自動分配newid()
。我怎樣才能做到這一點?目前,我有什麼拋出的'Conversion failed when converting from a character string to uniqueidentifier.'
我從你的代碼中假設這是SQL Server?請使用正確的RDBMS包括編輯您的問題和標籤。版!你說的是'MERGE',但我看不到任何東西要合併......是這樣嗎,已經有'#ExportTable'中的數據了,你必須合併(更新/插入)與XML中的數據?是隻有一個XML還是一次有很多XML?當然,這可以做得更好('FROM OPENXML'已過時),但我不完全明白你需要什麼... – Shnugo
這個問題解決了嗎?你需要進一步的幫助嗎?請允許我提示一個提示:如果這個問題已經解決,那麼在(最佳)答案的投票櫃檯下面勾選驗收檢查將會非常友善。這將1)標記此問題已解決2)使追隨者更容易找到最佳解決方案3)支付點給答覆者和4)支付點給你。既然你已經跨越了15分的邊界,你還需要額外的投票。這是SO的方式來說聲謝謝。快樂編碼! – Shnugo