在SQL中有一个数据库 db1 中的表 article 顺便问一下:存储过程中的 T_id 一定要是表中的一个字段吗?我的表中没有这个字段。
存储过程的代码如下:
CREATE proc page
@pagenum int
as
SET NOCOUNT ON
declare @sql nvarchar(500)
declare @pagecount int
declare @row_num int
begin
select @row_num=count(*) from article
create table #change (T_id int)
if @row_num>6
begin
set @row_num=@pagenum*6
if @row_num=6
select top 6 * from article
else
begin
set @row_num=(@pagenum-1)*6
set @pagecount=@row_num
set @sql=N'insert #change (T_id) select top '+cast(@pagecount as char(100))+' T_id from article where T_id not in (select T_id from #change)'
exec sp_executesql @sql
select top 6 * from article where T_id not in (select T_id from #change)
end
end
else
select * from article
end
GO
调用页面的第一页没有问题了,但是当点击“下一页”时出错,出错信息如下:
错误类型:
Microsoft OLE DB Provider for ODBC Drivers (0x80040E14)
/sqlnvxp/sqlpage.asp, 第 20 行
ASP页面调用如下:
1
2dim T_com
3dim T_rs
4dim parameters
5set T_com=server.createobject("adodb.command")
6T_com.ActiveConnection=conn
7T_com.CommandText="page"
8T_com.CommandType=adCMdStoredProc
9'T_com.Prepared=true
10set parameters=T_com.CreateParameter("@pagenum",adInteger,adParamInput)
11T_com.Parameters.Append parameters
12dim page
13page=request.QueryString("page")
14if page="" then
15page=1
16end if
17T_com("@pagenum")=page
18set T_rs=T_com.Execute '这一行出错,Microsoft OLE DB Provider for ODBC Drivers (0x80040E14)
19
20do while not T_rs.eof
21response.Write T_rs("writer")
22response.Write "
<br/>
1"
2T_rs.movenext
3loop
1<a href="sqlpage.asp?page=```
2=page+1
3```">下一页</a>
---------------------------------------------------------------
up
---------------------------------------------------------------
那你把那个字段加上去试一下
---------------------------------------------------------------
create table #change (T_id int)
??????
调一次建一个表?重复啊。
---------------------------------------------------------------
up
---------------------------------------------------------------
用过后最好drop table #change
---------------------------------------------------------------
改成#change不出错是因为#change里有T_id啊
存储过程里begin end之间的都是普通的sql语句,sql语句有问题当然会出错啦。
---------------------------------------------------------------
.
.
.
end
end
else
select * from article
drop table #change
end
GO
存储过程里加上这句试试
---------------------------------------------------------------
临时表 create table #change (T_id int)
不要放在存储过程中,放在存储过程之外试试
---------------------------------------------------------------
其实就是把article里的自增id暂存到临时表里的,临时表是存储过程级别的,所以一旦存储过程失效,临时表就自动删除
/我的表 article 中就没有 T_id 这个字段,但如果把 article 改成 #change 的话就不会提示出错,但点击“下一页”时不能取出数据,/
---你改成#change,肯定读不出数据啊,因为数据在article里,#change的意思就是存放不要取出的id号,然后从article里取出top 6(id不能和#change相同)
/set T_rs=T_com.Execute '这一行出错,Microsoft OLE DB Provider for ODBC Drivers (0x80040E14)/
--这是因为存储过程有错误哦
--set @sql=N'insert #change (T_id) select top '+cast(@pagecount as char(100))+' T_id from article where T_id not in (select T_id from #change)'把cast(@pagecount as char(100))改成cast(@pagecount as varchar)试试
---------------------------------------------------------------
还有就是只要从article中取出的id,用你数据库中的自增字段名,肯定不是T_id
---------------------------------------------------------------
首先,你每次执行存储过程都执行CREATE TABLE #CHANGE 肯定是不对的,第二次调用建表会发生冲突。数据暂存在临时表里可以,不再用时用其他方式删除临时表。
t_id字段的使用莫名其妙!表里没有是取不出来的,这是常识。
---------------------------------------------------------------
上面已经说了阿
我现在没有调试环境,只能先这样说了