I have a FOR XML PATH Stored Procedure that returns some XML in the usual manner (shortened for clarity):
CREATE PROCEDURE sp_returnsubnode
AS
BEGIN
SELECT subnode.subnodeid "@subnodeid"
,subnode.somedata "somedata"
FROM subnode
FOR XML PATH('subnode')
END
I have another stored procedure that I would like to include the results of the above query within e.g.
CREATE PROCEDURE sp_returnmainxml
AS
BEGIN
SELECT node.nodeid "@nodeid"
,node.nodedata "data"
,[AT THIS POINT I WOULD LIKE TO CALL sp_returnsubnode AND NEST IT]
,node.moredata "moredata"
FROM node
FOR XML PATH ('node')
END
But the methods I have tried like assigning the results of executing sp_subnode to an xml datatype and attempting to nest that have failed.
This seems like something people would want to do often but I haven't found any reference on how to do it. Is it even possible?
You can do this with a user defined functions that returns XML.
A function returning XML:
Use like this: