2017-01-23 74 views
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.'

+0

我從你的代碼中假設這是SQL Server?請使用正確的RDBMS包括編輯您的問題和標籤。版!你說的是'MERGE',但我看不到任何東西要合併......是這樣嗎,已經有'#ExportTable'中的數據了,你必須合併(更新/插入)與XML中的數據?是隻有一個XML還是一次有很多XML?當然,這可以做得更好('FROM OPENXML'已過時),但我不完全明白你需要什麼... – Shnugo

+0

這個問題解決了嗎?你需要進一步的幫助嗎?請允許我提示一個提示:如果這個問題已經解決,那麼在(最佳)答案的投票櫃檯下面勾選驗收檢查將會非常友善。這將1)標記此問題已解決2)使追隨者更容易找到最佳解決方案3)支付點給答覆者和4)支付點給你。既然你已經跨越了15分的邊界,你還需要額外的投票。這是SO的方式來說聲謝謝。快樂編碼! – Shnugo

回答

0

一個錯誤在我的評論,FROM OPENXML與相關的存儲過程告訴準備和刪除文件已經過時,不應該使用更多的(除了極少數例外存在)。以下向您展示如何使用現代XML方法從XML讀取數據。

我的魔法水晶球告訴我,你可能需要像這樣:

DECLARE @xml XML= 
N'<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>'; 

查詢檢查/text(),這是NULL,如果沒有內容,否則,你會得到一個空字符串,這對於一個GUID是不可搜索的。認識到這一點,我們可以在一個NULL - 值的情況下使用ISNULL並返回NEWID()

SELECT ISNULL(tmpl.value(N'(ExportTemplateId/text())[1]',N'uniqueidentifier'),NEWID()) AS ExportTemplateId 
     ,tmpl.value(N'(ExportTypeId/text())[1]',N'uniqueidentifier') AS ExportTypeId 
     ,tmpl.value(N'(TemplateDesc/text())[1]',N'nvarchar(max)') AS TemplateDesc 
     ,tmpl.value(N'(Active/text())[1]',N'bit') AS Active 
     --DataElements??? 
FROM @xml.nodes(N'/ArrayOfTemplate/Template') AS A(tmpl) 

您可能需要閱讀one of my previous answers有關空元素和空值的差異。