oracle和sqlserver数据库直接生成xml

 最近做项目,需要提供xml字符串给第三方,之后我试了试这两种数据库 都可以很简单的实现

       oracle:举例

                SELECT      XMLElement("rowdata",
              XMLElement("row",
                 XMLForest(
                           code AS "code",
                           name AS "name",
                          py_code AS "other"
                          )  )  )               
               FROM table1 where code='124';

      sqlserver:举例

              select * from table1 where code='124'  for xml RAW ('row'),elements, root('rowdata')

       注: sqlserver中的如果想让空的字段也显示出来,需要在elements后 加xsinil

    我见到很多人都采用在开发的程序中 用字符串拼接来实现生成xml,我举的例子要比字符串拼接的代码量减少很多,而且xml已经很多年应用。

    如有问题,欢迎大家进行沟通 QQ 165393839

原文地址:https://www.cnblogs.com/cnajian/p/2260118.html