大家帮我看看,我想在ASP中显示以下SQL语句查询出来的结果怎么写?

loveyoubody 2009-09-21 03:17:04
我的数据库表如下格式:里面数据先由网页客户端输入.
f_xh(自动增加) f_uesr f_bmid f_bhrq f_ip a b c d
1 0005 0005 2009920 127.0.0.1 2 2 2 2
2 0006 0006 2009921 127.0.0.5 2 1 3 5

我现在需要通过网页(.asp)显示出类似以下格式的结果:并可以导出exle表.
\\网页首先先显示查询条件, 类似"请输入日期:________________ 查询" \\然后按查询后可以显示以下结果,
商品编码 黄岐 华远 亲人
a 2 0 0
b 2 0 0
c 2 0 0
d 2 0 0


下面是我查询上面结果的sql语句方法,但是我不会应用结合在ASP网页上.....
select * into #qq from (
select * from
(
select [f_uesr],[商品编码]='a',[数量]=[a] ,[f_bhrq],[f_ip] from cs union all
select [f_uesr],[商品编码]='b',[数量]=[b] ,[f_bhrq],[f_ip] from cs union all
select [f_uesr],[商品编码]='c',[数量]=[c] ,[f_bhrq],[f_ip] from cs union all
select [f_uesr],[商品编码]='d',[数量]=[d] ,[f_bhrq],[f_ip] from cs

)t

)tt
order by [商品编码]
------------------------------------------------------------------------------

select isnull(商品编码,0) as 商品编码 ,
max(case f_uesr when '0005' then 数量 else 0 end) 黄岐,
max(case f_uesr when '0006' then 数量 else 0 end) 华远,
max(case f_uesr when '0007' then 数量 else 0 end) 亲人
from #qq where f_bhrq='2009920' and f_uesr='0005'
group by isnull(商品编码,0)

...全文
2211 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
homel 2009-09-21
  • 打赏
  • 举报
回复
1.创建表
create table   cs(if_xh int identity(1,1)  primary key,
f_uesr nvarchar(10),
f_bmid nvarchar(10),
f_ip nvarchar(30),
f_bhrq datetime,
a real,
b real,
c real,
d real)

insert into cs select '005','0005','127.0.0.1','2009-09-20',2,2,2,2
union all select '0006','0006','127.0.0.5','2009-09-21',2,1,3,5

2.带查询条件的asp页面
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%response.Buffer=true
response.Expires=0 %>

<!--#include file="../../common/menuinc.asp" -->
<!--#include file="../../common/public_function.asp" -->
<%sdate=request("searchdate")%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>
<body>
<form name="form1">
请输入日期:<input type="text" name="searchdate" value="<%if sdate<>""then%><%=sdate%><%end if%>">
<input type="submit" value="搜索">
</form>
<%
sql="select * into #qq from ( select * from (select [f_uesr],[product_code]='a',[qty]=[a] ,[f_bhrq],[f_ip] from cs union all select [f_uesr],[product_code]='b',[qty]=[b] ,[f_bhrq],[f_ip] from cs union all select [f_uesr],[product_code]='c',[qty]=[c] ,[f_bhrq],[f_ip] from cs" &""
sql=sql &" union all select [f_uesr],[product_code]='d',[qty]=[d] ,[f_bhrq],[f_ip] from cs )t )tt order by [product_code]"
conn.execute(sql)
set rs=server.CreateObject("Adodb.recordset")
if request("searchdate")<>"" then '判断搜索的日期
sql1="select isnull(product_code,0) as product_code , max(case f_uesr when '0005' then qty else 0 end) huangqi, max(case f_uesr when '0006' then qty else 0 end)huayuan, max(case f_uesr when '0007' then qty else 0 end) qinren from #qq" &""
sql1=sql1 &" where f_bhrq='"&request.QueryString("searchdate")&"' and f_uesr='0005' group by isnull(product_code,0)"
else
sql1="select isnull(product_code,0) as product_code , max(case f_uesr when '0005' then qty else 0 end) huangqi, max(case f_uesr when '0006' then qty else 0 end)huayuan, max(case f_uesr when '0007' then qty else 0 end) qinren from #qq where f_uesr='0005' group by isnull(product_code,0)"
end if
response.Write sql1
set rs=conn.execute(sql1)

if not rs.eof then%>
<table border="1">
<tr>
<td>序号</td>
<td>商品编号</td>
<td>黄岐</td>
<td>华远</td>
<td>亲人</td>

</tr>
<%i=1
do while not rs.eof%>
<tr>
<td><%=i%></td>
<td><%=rs(0)%></td>
<td><%=rs(1)%> </td>
<td><%=rs(2)%> </td>
<td><%=rs(3)%> </td>
<tr>
<%i=i+1
rs.movenext
loop

rs.close
set rs=nothing
conn.execute("DROP TABLE #qq")'将临时表删除
conn.close:set conn=nothing
%>
</table>
<%end if%>
</body>
</html>

执行结果:
序号 商品编号 黄岐 华远 亲人
1 a 2 0 0

2 b 2 0 0

3 c 2 0 0

4 d 2 0 0


homel 2009-09-21
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 loveyoubody 的回复:]
要晕了...............为什么我的就是报错呢?   homel  Q177201915. 帮我看一下行吗?
[/Quote]
看站短信息
loveyoubody 2009-09-21
  • 打赏
  • 举报
回复
要晕了...............为什么我的就是报错呢?   homel  Q177201915. 帮我看一下行吗?
homel 2009-09-21
  • 打赏
  • 举报
回复
loveyoubody,
数据库连接一般不会写错,看看你是不是路径写错了?
SQLServerName = "192.168.0.6"//我用的是IP,本机的话你可以直接下localhost

homel 2009-09-21
  • 打赏
  • 举报
回复
数据库连接文件?

<%Dim SQLServerName,SQLDBUserName,SQLDBPassword,SQLDBName,conn,connstr
SQLServerName = "192.168.0.6"
SQLDBUserName = "sa"
SQLDBPassword = "fiona"
SQLDBName = "test"
on Error resume next
Set conn = Server.CreateObject("ADODB.Connection")
connstr="Provider=sqloledb;user id="&SQLDBUserName&";password="&SQLDBPassword&";initial catalog="&SQLDBName&";data source="&SQLServerName&";"
conn.Open connstr
''-- Check Error Number Start --
if Err.Number<>0 then
response.write(err.description&"<br>Please re-config SQL Server!")
response.End()
end if
on Error Goto 0''--Check End--%>[
loveyoubody 2009-09-21
  • 打赏
  • 举报
回复
homel 能不能把你的数据库连接文件代码发给我看看, 我的报错的?
homel 2009-09-21
  • 打赏
  • 举报
回复
忘记了,把sql里面的 qq写成#qq,因为是用临时表,刚才测试忘记了
其实一般用asp读取数据的步骤为:
1.建立数据库连接 --就是你的数据库连接文件
2.建立记录集 set rs=server.createobject("adodb.recordset")
3.写你的sql语句,如果一行写不完的话,就用如下形式
sql="select ... from table " &""
sql=sql & "where ...."
此时可以在测试的时候response.write sql '来测试sql语句传递的变量什么是否right
4.打开记录集 rs.open sql,conn,1,1 --只读
5.在table中中循环显示
6.关闭记录集,释放资源
7.关闭数据库链接文件
homel 2009-09-21
  • 打赏
  • 举报
回复
<!--#include file="../../common/menuinc.asp" -->
<!--#include file="../../common/public_function.asp" -->
//记得替换数据库连接文件,和你第二句sql中的条件
homel 2009-09-21
  • 打赏
  • 举报
回复
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%response.Buffer=true
response.Expires=0 %>

<!--#include file="../../common/menuinc.asp" -->
<!--#include file="../../common/public_function.asp" -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>
<body>
<%

sql="select * into qq from ( select * from (select [f_uesr],[product_code]='a',[qty]=[a] ,[f_bhrq],[f_ip] from cs union all select [f_uesr],[product_code]='b',[qty]=[b] ,[f_bhrq],[f_ip] from cs union all select [f_uesr],[product_code]='c',[qty]=[c] ,[f_bhrq],[f_ip] from cs" &""
sql=sql &" union all select [f_uesr],[product_code]='d',[qty]=[d] ,[f_bhrq],[f_ip] from cs )t )tt order by [product_code]"
conn.execute(sql)
response.Write sql
set rs=server.CreateObject("Adodb.recordset")
sql1="select isnull(product_code,0) as product_code , max(case f_uesr when '0005' then qty else 0 end) huangqi, max(case f_uesr when '0006' then qty else 0 end)huayuan, max(case f_uesr when '0007' then qty else 0 end) qinren from qq where f_uesr='0005' group by isnull(product_code,0)"
set rs=conn.execute(sql1)
if not rs.eof then%>
<table border="1">
<tr>
<td>序号</td>
<td>黄岐</td>
<td>华远</td>
<td>亲人</td>

</tr>
<%do while not rs.eof%>
<tr>
<td><%=rs(0)%></td>
<td><%=rs(1)%> </td>
<td><%=rs(2)%> </td>
<td><%=rs(3)%> </td>
<tr>
<%rs.movenext
loop
rs.close
set rs=nothing
conn.execute("DROP TABLE qq")'将临时表删除
conn.close:set conn=nothing
%>
</table>
<%end if%>
</body>
</html>


--sql语句我有改动,因为我的os是英文版本的,不识别有些汉字,所以我全部转换成英文的写法了
loveyoubody 2009-09-21
  • 打赏
  • 举报
回复
homel 我在等你噶 帖子要沉了
homel 2009-09-21
  • 打赏
  • 举报
回复
我正在本机建立你的数据表测试
你等一下

MR丶CHAN 2009-09-21
  • 打赏
  • 举报
回复

<--#include file="cs.asp"-->
<%
set conn=server.CreateObject("adodb.connection")

sql="" ‘你的查询语句
set rs=conn.execute (sql)'执行查询
if not rs.eof then
%>
<table>
<tr>
<td>商品编码</td>
<td>黄岐</td>
<td>华远</td>
<td>亲人</td>
</tr>
<% do while not rs.eof '循环%>
<tr>
<td><%=rs("字段1")%></td>
<td><%=rs("字段2")%></td>
<td><%=rs("字段3")%></td>
<td><%=rs("字段4")%></td>
</tr>
<%
rs.movenext
loop
set rs=nothing
set conn=nothing '如果不再需要数据连接就关掉
end if
%>
</table>
loveyoubody 2009-09-21
  • 打赏
  • 举报
回复
实在是不知道怎么下手,我现在也只能 做到 2,SQL语句设计
shenzhenNBA 2009-09-21
  • 打赏
  • 举报
回复
dim rs
set rs=server.createobject("adodb.recordset")
sql="..."
rs.open sql,conn,1,1
...



shenzhenNBA 2009-09-21
  • 打赏
  • 举报
回复
步骤:
1,接收,获取条件
2,SQL语句设计
3,(数据库连接)RS对象打开SQL语句
4,显示数据
loveyoubody 2009-09-21
  • 打赏
  • 举报
回复
<!--#include file="cs.asp"-->
<%set rs=server.createobject("adodb.recordset")
sql1="select * into #qq from ( select * from (select [f_uesr],[商品编码]='a',[数量]=[a] ,[f_bhrq],[f_ip] from cs union all select [f_uesr],[商品编码]='b',[数量]=[b] ,[f_bhrq],[f_ip] from cs union all select [f_uesr],[商品编码]='c',[数量]=[c] ,[f_bhrq],[f_ip] from cs union all select [f_uesr],[商品编码]='d',[数量]=[d] ,[f_bhrq],[f_ip] from cs )t )tt order by [商品编码]"
sql2="select isnull(商品编码,0) as 商品编码 , max(case f_uesr when '0005' then 数量 else 0 end) 黄岐, max(case f_uesr when '0006' then 数量 else 0 end) 华远, max(case f_uesr when '0007' then 数量 else 0 end) 亲人 from #qq where f_bhrq='2009920' and f_uesr='0005' group by isnull(商品编码,0)"
rs.open sql1,conn,1,1
rs.open sql2,conn,1,1
if not rs.eof then%>
<table>
<tr> <td> </td> </tr>
<%do while not rs.eof%>
<tr>
<td> <%=rs("")%> </td>
....
<tr>
<%rs.movenext
loop
rs.close
set rs=nothing%>
<%end if%>
loveyoubody 2009-09-21
  • 打赏
  • 举报
回复
楼上,麻烦再看看吧。为什么显示:

ADODB.Recordset 错误 '800a0bb9'

参数类型不正确,或不在可以接受的范围之内,或与其他参数冲突。

/复件 incul.asp,行 5
homel 2009-09-21
  • 打赏
  • 举报
回复
既然lz sql语句已经出来了就按下面的做啊:
<!--#include file="**.asp"-->
<%set rs=server.createobject("adodb.recordset")
sql1="你的第一句sql"
sql2="你的第二句摄取量"
rs.open sql1,conn,1,1
rs.open sql2,conn,1,1
if not rs.eof then%>
<table>
<tr><td></td></tr>
<%do while not rs.eof%>
<tr>
<td><%=rs("")%></td>
....
<tr>
<%rs.movenext
loop
rs.close
set rs=nothing%>
<%end if%>


28,406

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧