这是我拥有的示例访问查询(查询参考/名称 - 以上查询)
select column_date, sum(field1), sum(field2) from table
group by column_date
union all
select 'Total' as column_date, sum(field1), sum(field2) from table
当我尝试在 asp 中使用以下代码访问浏览器中的 SQL 查询时,它只显示标题。
<html>
<body>
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.open "odbcdatasourcename"
set rs = Server.CreateObject("ADODB.recordset")
sql="SELECT * from abovequery"
rs.Open sql, conn
%>
<table border="1" width="100%">
<tr>
<%for each x in rs.Fields
response.write("<th>" & x.name & "</th>")
next%>
</tr>
<%do until rs.EOF%>
<tr>
<%for each x in rs.Fields%>
<td><%Response.Write(x.value)%></td>
<%next
rs.MoveNext%>
</tr>
<%loop
rs.close
conn.close
%>
</table>
</body>
</html>
请注意,当我将上述查询的输出复制到表中并使用相同的 asp 代码时,它可以正常工作。所以显然这是在 asp 中执行“UNION”查询的问题。我想知道是否有任何解决方案。当我尝试执行交叉表查询时也会发生同样的问题。
新:这是我现在尝试的
- 但它的最后一行总计未与其他主要数据行正确对齐。
这是我现在创建的示例访问查询
第一个查询 - query1
select column_date, sum(field1), sum(field2) from table
group by column_date
第二个查询 - query2
select 'Total' as column_date, sum(field1), sum(field2) from table
ASP代码
<html>
<body>
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.open "odbcdatasourcename"
set rs = Server.CreateObject("ADODB.recordset")
sql="SELECT * from query1"
rs.Open sql, conn
%>
<table border="1" width="100%">
<tr>
<%for each x in rs.Fields
response.write("<th>" & x.name & "</th>")
next%>
</tr>
<%do until rs.EOF%>
<tr>
<%for each x in rs.Fields%>
<td><%Response.Write(x.value)%></td>
<%next
rs.MoveNext%>
</tr>
<%loop
rs.close
conn.close
%>
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.open "odbcdatasourcename"
set rs = Server.CreateObject("ADODB.recordset")
sql="SELECT * from query2"
rs.Open sql, conn
%>
<table border="1" width="100%">
<%for each x in rs.Fields%>
<td><%Response.Write(x.value)%></td>
<%next
rs.MoveNext%>
</tr>
<%loop
rs.close
conn.close
%>
</table>
</body>
</html>
这是我第三次尝试解决对齐问题
<html>
<body>
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.open "odbcdatasourcename"
set rs = Server.CreateObject("ADODB.recordset")
sql="SELECT * from query1"
rs.Open sql, conn
%>
<table border="1" width="100%">
<tr>
<%for each x in rs.Fields
response.write("<th>" & x.name & "</th>")
next%>
</tr>
<%do until rs.EOF%>
<tr>
<%for each x in rs.Fields%>
<td><%Response.Write(x.value)%></td>
<%next
rs.MoveNext%>
</tr>
<%
set rs = Server.CreateObject("ADODB.recordset")
sql="SELECT * from query2"
rs.Open sql, conn
%>
<%for each x in rs.Fields%>
<td><%Response.Write(x.value)%></td>
<%next
rs.MoveNext%>
</tr>
<%loop
rs.close
conn.close
%>
</table>
</body>
</html>