SAP Sybase ASA SQL语句直接生成HTML结果

时间:2021-12-15 11:49:18

在ASA数据库中,output, input子句并不能直接用于SQL语句中,只能用于isql交互命令行里。因此要在存储过程或者程序里生成html格式的结果,还需要自己实现,但也不难。

如:

select * from t

得到结果:

id,col2
1,'中国'
2,'spring'

输出为html格式:
select * from t ; 
output to "a.html" format HTML;

  • 执行时间: 0.016 秒
  • 正在将数据导出到 "D:\asa120\BIN32\a.html"
  • 2 行已写入 "D:\asa120\BIN32\a.html" 
a.html内容为:

<html>
<head>
<META content="text/html;charset=GBK">
</head>
<body>
<table border>
<tr><th>id</th><th>col2</th></tr>
<tr><td>1</td><td>中国</td></tr>

<tr><td>2</td><td>spring</td></tr>

</table>
</body>
</html>

我们可以用SQL语句为之生成这样的结果:

select xmlelement(name "html",
            (xmlelement (name "body", 
            xmlelement(name "thead",
             xmlelement(name "tr",
                xmlelement(name "th",  'id'),
                xmlelement(name "th", 'col2')
             )
            ),
        xmlelement(name "tbody",
            xmlagg( xmlelement (name "tr",
                       xmlelement(name "td", id),
                       xmlelement(name "td", col2)
                   )))))
) from t;


结果是这样的:

<html><body><thead><tr><th>id</th><th>col2</th></tr></thead><tbody><tr><td>1</td><td>中国</td></tr><tr><td>2</td><td>spring</td></tr></tbody></body></html>
这个结果并没有输出html文件的head, META等信息,下边是一个完整的sql:

select xmlelement(name "html",
            xmlelement (name "head", 
                xmlelement(name "title", 't表转换'),
                xmlelement(name "META", xmlattributes('Content-Type' as "http-equiv" , 'text/html;charset=GBK' as "content"))
            ),
            (xmlelement (name "body", 
                xmlelement(name "thead",
                    xmlelement(name "tr",
                    xmlelement(name "th",  'id'),
                    xmlelement(name "th", 'col2')
                )
                ),
                xmlelement(name "tbody",
                    xmlagg( xmlelement (name "tr",
                       xmlelement(name "td", id),
                       xmlelement(name "td", col2)
                    )))))
) from t;


得到的html内容如下:

<html>
  <head>
    <title>t表转换</title>
    <META http-equiv="Content-Type"
    content="text/html;charset=GBK" />
  </head>
  <body>
    <thead>
      <tr>
        <th>id</th>
        <th>col2</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>1</td>
        <td>中国</td>
      </tr>
      <tr>
        <td>2</td>
        <td>spring</td>
      </tr>
    </tbody>
  </body>
</html>

仔细一看,上边的结果少了<table border="1">.....</table>

加上xmlelement(name "table", xmlattributes('1' as "border")),即可

完整的sql如下:

 
 
select xmlelement(name "html",    
            xmlelement (name "head",     
                xmlelement(name "title", 't表转换'),    
                xmlelement(name "META", xmlattributes('Content-Type' as "http-equiv" , 'text/html;charset=GBK' as "content"))    
            ),    
            (xmlelement (name "body",  
                xmlelement(name "table", xmlattributes('1' as "border"),      
                    xmlelement(name "thead",    
                        xmlelement(name "tr",    
                        xmlelement(name "th",  'id'),    
                        xmlelement(name "th", 'col2')    
                        )    
                    ),    
                    xmlelement(name "tbody",    
                        xmlagg( xmlelement (name "tr",    
                           xmlelement(name "td", id),    
                           xmlelement(name "td", col2)    
                                )  
                               )  
                           )  
                    )
                )  
            )   
) from t;    


最终得到的结果如下:

<html>
  <head>
    <title>t表转换</title>
    <META http-equiv="Content-Type"
    content="text/html;charset=GBK" />
  </head>
  <body>
    <table border="1" >
    <thead>
      <tr>
        <th>id</th>
        <th>col2</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>1</td>
        <td>中国</td>
      </tr>
      <tr>
        <td>2</td>
        <td>spring</td>
      </tr>
    </tbody>
   </table>
  </body>
</html>