Försöker få en Stored Procedure som ska retunera hierarkisk data i XML format. Löste sig, WHERE N.node_parentid = '0' ska vara WHERE N.node_parentid is null. Små klantigt :)FOR XML EXPLICIT problem
Tabellen den hämtar ifrån ser ut så här:
<code>
node_guid uniqueidentifier 16 0
node_parentid uniqueidentifier 16 1
node_templateid int 4 0
node_position int 4 0
node_visible bit 1 0
node_name nvarchar 50 0
node_url nvarchar 255 1
</code>
Och SQL jag försöker få att fungera ser ut så här:
<code>
SELECT 1 as tag, <- FEL
null as parent,
'' as [root!1!id],
null as [treeLevel!2!id],
null as [treeLevel!2!name],
null as [treeLevel!2!nodeOrder],
null as [treeLevel!3!id],
null as [treeLevel!3!name],
null as [treeLevel!3!nodeOrder]
UNION ALL
SELECT 2 as tag,
1 as parent,
'' as [root!1!id],
N.node_guid as [treeLevel!2!id],
N.node_name as [treeLevel!2!name],
N.node_position as [treeLevel!2!nodeOrder],
null as [treeLevel!3!id],
null as [treeLevel!3!name],
null as [treeLevel!3!nodeOrder]
FROM Nodes N
WHERE N.node_parentid is null
UNION ALL
SELECT 3 as tag,
2 as parent,
'' as [root!1!id],
N.node_guid as [treeLevel!2!id],
N.node_name as [treeLevel!2!name],
N.node_position as [treeLevel!2!nodeOrder],
N2.node_guid as [treeLevel!3!id],
N2.node_name as [treeLevel!3!name],
N2.node_position as [treeLevel!3!nodeOrder]
FROM Nodes N
inner join Nodes N2 on
(N2.node_parentid = N.node_guid)
WHERE N.node_parentid = '0'
ORDER BY [root!1!id],
[treeLevel!2!nodeOrder],
[treeLevel!2!name],
[treeLevel!2!id],
[treeLevel!3!nodeOrder],
[treeLevel!3!name],
[treeLevel!3!id]
FOR XML EXPLICIT
</code>
Felmeddelandet jag får är:
Server: Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near 'uniqueidentifier'.
Har testat att i byta ut 1:an mot en uniqueidentifier men det gör ingen skillnad. Går detta ens att göra med uniqueidentifiers? Vad kan vara fel?
Tacksam på förhand!Sv: FOR XML EXPLICIT problem