我正在用Oracle 10g构建一个sql查询, 并希望以xml格式获得sql输出:
<Users>
<User>
<USRID>1234</USRID>
<USERNAME>ABCD</USERNAME>
<ROLES>
<ROLE>
<ROLEID>1</ROLEID>
<ROLENAME>GlobalAdministrator</ROLENAME>
<ISDEFAULTROLE>1</ISDEFAULTROLE>
</ROLE>
<ROLE>
<ROLEID>2</ROLEID>
<ROLENAME>Administrator</ROLENAME>
<ISDEFAULTROLE>0</ISDEFAULTROLE>
</ROLE>
</ROLES>
</User>
<User>
<USRID>2312</USRID>
<USERNAME>XUX</USERNAME>
<ROLES>
<ROLE>
<ROLEID>3</ROLEID>
<ROLENAME>AccountManager</ROLENAME>
<ISDEFAULTROLE>1</ISDEFAULTROLE>
</ROLE>
<ROLE>
<ROLEID>5</ROLEID>
<ROLENAME>Approver</ROLENAME>
<ISDEFAULTROLE>0</ISDEFAULTROLE>
</ROLE>
</ROLES>
</User>
</Users>
SELECT XMLElement("Users",
XMLAgg(XMLElement("User",
XMLElement("USRID",u.USRID) ,
XMLElement("USERNAME",u.USERNAME) ,
XMLElement("Roles",
SELECT xmlagg(XMLElement("Role",
XMLElement("ROLEID",ur.ROLEID),
XMLElement("ROLENAME",r.NAME),
xmlelement("ISDEFAULTROLE",ur.PRIMARY))
FROM USERROLE ur,role r WHERE u.USRID = ur.USRID AND r.ROLEID = ur.ROLEID))))).getClobVal()
FROM usr u
但它给出了以下错误:
ORA-00936:缺少表达
00936. 00000 - “缺少表达”
*原因:
*行动:
行错误:26列:1
我该怎么做?
答案 0 :(得分:0)
我已经弄明白了:
SELECT XMLElement("Users",
XMLAgg(XMLElement("User"
,XMLElement("USRID",u.USRID)
,XMLElement("USERNAME",u.USERNAME)
,xmlElement("Roles",
(
select xmlAgg(xmlElement("Role"
,XMLElement("ROLEID",ur.ROLEID)
,XMLElement("ROLENAME",r.NAME)
,xmlelement("ISDEFAULTROLE",ur.PRIMARY)))
from USERROLE ur,role r where u.USRID = ur.USRID and r.ROLEID = ur.ROLEID)
)))).getClobVal()
from usr u