February 15, 2017 at 4:08 am
Dear Everyone
I am trying to format the XML data correctly in my SSMS but when i try to run the following query:
USE AdventureWorks2012;
GO
SELECT TOP 2
Sales.SalesOrderIDAS SalesID,
Territory.name AS TerritoryName,
Country.name AS CountryName
FROM Sales.SalesOrderHeaderSales
INNER JOIN Sales.SalesTerritoryTerritory
ONSales.TerritoryID =Territory.TerritoryID
INNER JOIN Person.CountryRegionCountry
ONTerritory.CountryRegionCode = Country.CountryRegionCode
FOR XML RAW;
i am supposed to get the following result in 2 lines
<rowSalesID="43664" TerritoryName="Northwest"CountryName="United States" />
<row SalesID="43665" TerritoryName="Northwest"CountryName="United States" />
Yes i get the result as 1 line
Im guessing its an option i have to enable or check but where do i do this?
kindly advise
thanks
Kal
February 15, 2017 at 9:29 am
hurricaneDBA - Wednesday, February 15, 2017 4:08 AMDear Everyone
I am trying to format the XML data correctly in my SSMS but when i try to run the following query:USE AdventureWorks2012;
GO
SELECT TOP 2
Sales.SalesOrderIDAS SalesID,
Territory.name AS TerritoryName,
Country.name AS CountryName
FROM Sales.SalesOrderHeaderSales
INNER JOIN Sales.SalesTerritoryTerritory
ONSales.TerritoryID =Territory.TerritoryID
INNER JOIN Person.CountryRegionCountry
ONTerritory.CountryRegionCode = Country.CountryRegionCode
FOR XML RAW;
i am supposed to get the following result in 2 lines<rowSalesID="43664" TerritoryName="Northwest"CountryName="United States" />
<row SalesID="43665" TerritoryName="Northwest"CountryName="United States" />
Yes i get the result as 1 line
Im guessing its an option i have to enable or check but where do i do this?
kindly advise
thanks
Kal
Sorry, but that's not an option that I know of. You have the two records in XML format, but entirely within one output row of a query. If you want to then further process that XML, you can query it. It would be impractical to query it if it were separated into two distinct rows.
Steve (aka sgmunson) 🙂 🙂 🙂
Rent Servers for Income (picks and shovels strategy)
February 15, 2017 at 9:53 am
You'd need to do it as a subquery:SELECT TOP 2 CAST((SELECT Sales.SalesOrderID AS SalesID,
Territory.name AS TerritoryName,
Country.name AS CountryName
FROM Sales.SalesOrderHeader Salessq
INNER JOIN Sales.SalesTerritory Territory ON Sales.TerritoryID =Territory.TerritoryID
INNER JOIN Person.CountryRegion Country ON Territory.CountryRegionCode = Country.CountryRegionCode
WHERE Salessq.SalesOrderID = Sales.SalesOrderID
FOR XML RAW) AS xml) AS SalesOrderXML
FROM Sales.SalesOrderHeader Sales
Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
Larnu.uk
February 15, 2017 at 9:52 pm
sgmunson - Wednesday, February 15, 2017 9:29 AMSorry, but that's not an option that I know of. You have the two records in XML format, but entirely within one output row of a query. If you want to then further process that XML, you can query it. It would be impractical to query it if it were separated into two distinct rows.
Don't think "rows"... think "lines" for this. It's for readability purposes by humans when the output is directed to a file.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply