简体   繁体   中英

SQL Server Assign XML Variable from Row Data with Namespaces

So I have an entire XML file stored in a single row/column of a table. I need to assign that to a variable and specify WITH NAMESPACES. I can't come up with any syntax that works though.

DECLARE @xmlText xml

 set  @xmlText = (select rowdata    
 FROM dbo.Staging with (nolock)    
 WHERE     
  FileId = @FileID AND    
  ClientID = @ClientID) 

Will I somehow have to contrive a sql statement for execution? If I do though, I'm not sure how I'd get that assigned back to a local variable since it would be on another spid. I'm stumped!


I will be using xquery :)

This passed syntax check...

;WITH XMLNAMESPACES('http://xxx' AS p) 
(select @xmlText = rowdata    
 FROM dbo.Staging with (nolock)    
 WHERE     
  FileId = @FileID AND    
  ClientID = @ClientID) 


SELECT      
MFRRequestID = Y.i.value('(@id)[1]', 'int'),     
RequestStatus = Y.i.value('Status[1]', 'varchar(10)') ,     
AccountNumber = Y.i.value('p:AccountNumber[1]', 'varchar(10)'),    
ReferralDate = CIF.value('(ReferralDate)[1]', 'datetime'),     
SuspenseBalance = CIF.value('(SuspenseBalance)[1]', 'varchar(25)') 
FROM      @xmltext.nodes('/p:OrderRequest/p:RequestItems/p:MFRRequest') 
AS Y(i) CROSS APPLY     i.nodes('p:CaseInformation') AS Tbl(CIF) 

But I am getting the error...

XQuery [nodes()]: The name "p" does not denote a namespace.

This is related to a question I asked yesterday. I basically got this exact thing working but instead of assigning the @xmltext variable from a table (which is what I need to do for the final production code), I copied and pasted the xml and assigned it as a hardcoded string just so I could work out the xquery syntax. Sorry for the confusion, this is all pretty new to me.

It's your second query that needs WITH XMLNAMESPACES .

DECLARE @xmlText xml;

SELECT @xmlText = rowdata    
FROM dbo.Staging
WHERE     
  FileId = @FileID AND    
  ClientID = @ClientID; 


WITH XMLNAMESPACES('http://xxx' AS p)
SELECT      
MFRRequestID = Y.i.value('(@id)[1]', 'int'),     
RequestStatus = Y.i.value('Status[1]', 'varchar(10)') ,     
AccountNumber = Y.i.value('p:AccountNumber[1]', 'varchar(10)'),    
ReferralDate = CIF.value('(ReferralDate)[1]', 'datetime'),     
SuspenseBalance = CIF.value('(SuspenseBalance)[1]', 'varchar(25)') 
FROM      @xmltext.nodes('/p:OrderRequest/p:RequestItems/p:MFRRequest') 
AS Y(i) CROSS APPLY     i.nodes('p:CaseInformation') AS Tbl(CIF);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM