I have the below Query that returns the information that I need, but I need to use this query In a Scalar Valued function to use the returned values in a computed column.
The XML Column is in the same table and I need to insert the values from Settings into a column named Directions
;WITH XMLNAMESPACES ( 'http://www.w3.org/2001/XMLSchema' AS als )
SELECT
a.a.value('@Settings', 'VARCHAR(50)') AS [Settings]
FROM Base AS X
CROSS APPLY X.BaseXML.nodes('als:Name') a(a)
The Function I was trying, but not getting anywhere was
CREATE FUNCTION [dbo].[ChooseRevision](@lineId int) Returns integer As
Begin
Return (WITH XMLNAMESPACES ( 'http://www.w3.org/2001/XMLSchema' AS als )
SELECT
a.a.value('@Settings', 'VARCHAR(50)') AS [Settings]
FROM Base AS X
CROSS APPLY X.BaseXML.nodes('als:Name') a(a)
Where LineId = @lideid
)
End
GO
How would you include the query inside a scarlar function to use as a computed column?
<als:Doc xmlns:als="http://www.w3.org/2001/XMLSchema" SchemaVersion="0.1" Settings="First Test" Title="Recovery Loop">
<als:Base Rev="0" Id="201" />
<als:Number TimeStamp="2013-01-21T15:08:00">
<als:Member Name="Club Run" DirId="123" />
</als:Number>
</als:Doc>
Assuming there is only one
als:Namenode per XML value you can write this:If you are planning to use this function within the dbo.Base table itself, you should use this version instead: