在asp中使用unionall或交叉表访问查询不起作用

时间:2012-07-03 20:47:24

标签: ms-access asp-classic ms-access-2007

以下是我的示例访问查询(查询引用/名称 - 上述查询)

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”查询的问题。我想知道是否有任何解决方案。当我尝试执行交叉表查询时也会出现同样的问题。

新:这是我现在尝试的

  • 但其与total的最后一行未与其他主数据行正确对齐。

以下是我现在创建的示例访问查询

第一个查询 - 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>

1 个答案:

答案 0 :(得分:0)

您的说明听起来像记录集中没有记录。确认这一点。

<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, 3 ' adOpenStatic '
Response.Write("RecordCount: " & rs.RecordCount)
rs.close
conn.close
%>
</body>
</html>