Question

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?

Était-ce utile?

La solution

You can do this with a user defined functions that returns XML.

A function returning XML:

create function getsubnode(@P int)
returns xml as
begin
  return (
          select @P as '@subnodeid',
                 'SubNodData' as somedata
          for xml path('subnode'), type
         ) 
end

Use like this:

select nodeid as '@nodeid',
       nodedata as data,
       dbo.getsubnode(nodeid),
       moredata
from node
for xml path('node')
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top