Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12

Flatten a one to many? Expand / Collapse
Author
Message
Posted Friday, March 8, 2013 12:05 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, July 10, 2014 5:12 PM
Points: 33, Visits: 98
Okay all, just got out of a meeting and went back to it. I think I have it all sorted except for one, tiny portion:
, 'StorageLocation' 
= STUFF
(
(
SELECT ','
+ COALESCE('Rm: ' + lbr2.Room, '')
+ COALESCE(', Cab: ' + ll2.fkCabinet, '')
+ COALESCE(', Bin: ' + CAST(DecryptByKey(ll2.Bin) AS varchar(20)), '')
+ COALESCE(', Box: ' + CAST(DecryptByKey(ll2.Box) AS varchar(20)), '')
+ COALESCE(', ' + CAST(DecryptByKey(ll2.StartColumn) AS varchar(2))
+ CAST(DecryptByKey(ll2.StartRow) AS varchar(2)), '')
+ COALESCE(' --> ' + CAST(DecryptByKey(ll2.EndColumn) AS varchar(2))
+ CAST(DecryptByKey(ll2.EndRow) AS varchar(2)), '')
FROM
dbo.tblLotLocation AS ll2
INNER JOIN
dbo.tblLocationBldgRoom AS lbr2 ON lbr2.pkLocationBldgRoomId = ll2.fkLocationBldgRoomId
WHERE
ll2.fkItemLotId = od.fkItemLotId
ORDER BY
pkLotLocationId
FOR XML PATH(''),
TYPE
).value('.','varchar(max)')
,1
,1
,''
)

This is the section I am a bit stuck on:
).value('.','varchar(max)')

I have read that .value() is an XQury function that accepts an XML data type and converts it to SQL Server data type. Makes sense.
From MSDN: value (XQuery, SQLType), where "XQuery - is the XQuery expression, a string literal, that retrieves data inside the XML instance" and "SQLType - Is the preferred SQL type."

But what is the XQuery '.' literal actually doing? Can I *assume* that since an empty string is sent to
FOR XML PATH('')

no root node name is created, and that the '.' argument in the XQuery is just looking at the unnamed root for its data?

Thanks.

Kurt
Post #1428726
Posted Friday, March 8, 2013 1:33 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 6:57 AM
Points: 7,084, Visits: 12,577
But what is the XQuery '.' literal actually doing? Can I *assume* that since an empty string is sent to
FOR XML PATH('')

no root node name is created, and that the '.' argument in the XQuery is just looking at the unnamed root for its data?


As a test, change your code to look like this and see if it makes more sense to see that the FOR XML piece of the query is actually returning an XML document.

The .value is actually just picking up the contents of the root level node of that document, i.e. the '.' node, and casting it to a VARCHAR(MAX).

, 'StorageLocation' 
=
(
SELECT ','
+ COALESCE('Rm: ' + lbr2.Room, '')
+ COALESCE(', Cab: ' + ll2.fkCabinet, '')
+ COALESCE(', Bin: ' + CAST(DecryptByKey(ll2.Bin) AS varchar(20)), '')
+ COALESCE(', Box: ' + CAST(DecryptByKey(ll2.Box) AS varchar(20)), '')
+ COALESCE(', ' + CAST(DecryptByKey(ll2.StartColumn) AS varchar(2))
+ CAST(DecryptByKey(ll2.StartRow) AS varchar(2)), '')
+ COALESCE(' --> ' + CAST(DecryptByKey(ll2.EndColumn) AS varchar(2))
+ CAST(DecryptByKey(ll2.EndRow) AS varchar(2)), '')
FROM
dbo.tblLotLocation AS ll2
INNER JOIN
dbo.tblLocationBldgRoom AS lbr2 ON lbr2.pkLocationBldgRoomId = ll2.fkLocationBldgRoomId
WHERE
ll2.fkItemLotId = od.fkItemLotId
ORDER BY
pkLotLocationId
FOR XML PATH(''),
TYPE
)



edit: add quote


__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Post #1428755
Posted Friday, March 8, 2013 1:54 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, July 10, 2014 5:12 PM
Points: 33, Visits: 98
Excellent! That's what I thought. Thanks.

Kurt
Post #1428766
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse