Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Need better formatting on this one

Author  Topic 

SMerrill
Posting Yak Master

206 Posts

Posted - 2010-06-04 : 00:57:31
The result set comes out as "CopiesColorDuplexCollate" and I need it to come out as "{'Copies','Color','Duplex','Collate'}".
How do I format the XPath or SQL?

DECLARE @X    XML;
DECLARE @IDOC INT;
SELECT @X = '
<COMMAND SEQUENCENUM="1" ISSUEDFROM="IDM-SCCM2" STARTTIME="06-03-2010T21:31:19" EVERYCOUNT="0">
<RESULTS NODE="IDM-SCCM2">
<CIM>
<INSTANCE CLASSNAME="Win32_Printer">
<PROPERTY.ARRAY NAME="CapabilityDescriptions" TYPE="string">
<VALUE.ARRAY>
<VALUE>Copies</VALUE>
<VALUE>Color</VALUE>
<VALUE>Duplex</VALUE>
<VALUE>Collate</VALUE>
</VALUE.ARRAY>
</PROPERTY.ARRAY>
<PROPERTY NAME="Caption" TYPE="string">
<VALUE>Microsoft XPS Document Writer</VALUE>
</PROPERTY>
<PROPERTY NAME="Location" PROPAGATED="true" TYPE="string"></PROPERTY>
<PROPERTY NAME="MarkingTechnology" PROPAGATED="true" TYPE="uint16"></PROPERTY>
<PROPERTY NAME="MaxCopies" PROPAGATED="true" TYPE="uint32"></PROPERTY>
</INSTANCE>
</CIM>
</RESULTS>
</COMMAND>'


EXEC sp_xml_PrepareDocument @idoc OUTPUT, @X;
SELECT
Property
, VarType
, CASE LTRIM(RTRIM(Value))
WHEN 'NULL' THEN NULL
ELSE Value
END as Value
FROM OPENXML (@idoc, '//PROPERTY|//PROPERTY.ARRAY')
WITH (
Property VARCHAR(200) './@NAME'
, VarType VARCHAR(200) './@TYPE'
, Value VARCHAR(200) './VALUE|./VALUE.ARRAY'
)
EXEC sp_xml_RemoveDocument @idoc;

RESULTS LOOK LIKE THIS:

Property VarType Value
CapabilityDescriptions string CopiesColorDuplexCollate
Caption string Microsoft XPS Document Writer
Location string NULL
MarkingTechnology uint16 NULL
MaxCopies uint32 NULL


~ Shaun Merrill
Seattle area
   

- Advertisement -