老调试不成功的存储过程,一个堵我心口好久的问题。

wnglish 2003-09-29 01:50:16
该存储过程老说:“@indextable没有声明”,我明明声明了,晕倒。

CREATE procedure s_Tables_Page
(
@pagesize int,
@pageindex int,
@table varchar(200)
)
as
set nocount on
declare @indextable table(id int identity(1,1),nid int)
declare @PageLowerBound int
declare @PageUpperBound int
set @PageLowerBound=(@pageindex-1)*@pagesize
set @PageUpperBound=@PageLowerBound+@pagesize
set rowcount @PageUpperBound
exec ('insert into '+@indextable+'(nid) select id from '+@table+' order by id desc')
exec ('select * from '+@table+' O,'+@indextable+'t where O.id=t.nid and t.id>'+@PageLowerBound+' and t.id<='+@PageUpperBound+' order by t.id')

set nocount off
GO
...全文
38 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
LoveSQL 2003-09-29
  • 打赏
  • 举报
回复
这个问题我也遇到过,@indextable这种表变量不能用在动态sql中。
可以定义一个表或临时表来替换一下就可以了。
pengdali 2003-09-29
  • 打赏
  • 举报
回复
CREATE procedure s_Tables_Page
(
@pagesize int,
@pageindex int,
@table varchar(200)
)
as
set nocount on
create table #indextable (id int identity(1,1),nid int)
declare @PageLowerBound int
declare @PageUpperBound int
set @PageLowerBound=(@pageindex-1)*@pagesize
set @PageUpperBound=@PageLowerBound+@pagesize
set rowcount @PageUpperBound
exec ('insert #indextable (nid) select id from '+@table+' order by id desc')
declare @a varchar(8000)
set @a='select * from '+@table+' O,#indextable t where O.id=t.nid and t.id>'+cast(@PageLowerBound as varchar(10))+' and t.id<='+cast(@PageUpperBound as varchar(10))+' order by t.id'
exec(@a)
set nocount off
GO
sdhdy 2003-09-29
  • 打赏
  • 举报
回复
CREATE procedure s_Tables_Page
(
@pagesize int,
@pageindex int,
@table varchar(200)
)
as
set nocount on
--declare @indextable table(id int identity(1,1),nid int)
declare @PageLowerBound int
declare @PageUpperBound int
set @PageLowerBound=(@pageindex-1)*@pagesize
set @PageUpperBound=@PageLowerBound+@pagesize
set rowcount @PageUpperBound
exec ('declare @indextable table(id int identity(1,1),nid int) insert into '+@indextable+'(nid) select id from '+@table+' order by id desc select * from '+@table+' O,'+@indextable+'t where O.id=t.nid and t.id>'+@PageLowerBound+' and t.id<='+@PageUpperBound+' order by t.id')
set nocount off
GO
sdhdy 2003-09-29
  • 打赏
  • 举报
回复
CREATE procedure s_Tables_Page
(
@pagesize int,
@pageindex int,
@table varchar(200)
)
as
set nocount on
declare @indextable table(id int identity(1,1),nid int)
declare @PageLowerBound int
declare @PageUpperBound int
set @PageLowerBound=(@pageindex-1)*@pagesize
set @PageUpperBound=@PageLowerBound+@pagesize
set rowcount @PageUpperBound
exec ('declare @indextable table(id int identity(1,1),nid int) insert into '+@indextable+'(nid) select id from '+@table+' order by id desc select * from '+@table+' O,'+@indextable+'t where O.id=t.nid and t.id>'+@PageLowerBound+' and t.id<='+@PageUpperBound+' order by t.id')
set nocount off
GO
aierong 2003-09-29
  • 打赏
  • 举报
回复
需要先申明
aierong 2003-09-29
  • 打赏
  • 举报
回复

CREATE procedure s_Tables_Page
(
@pagesize int,
@pageindex int,
@table varchar(200)
)
as
set nocount on
declare @indextable table(id int identity(1,1),nid int)
declare @PageLowerBound int
declare @PageUpperBound int
declare @sql varchar(4000),@sql1 varchar(1000)
set @PageLowerBound=(@pageindex-1)*@pagesize
set @PageUpperBound=@PageLowerBound+@pagesize
set rowcount @PageUpperBound
select @sql='insert into '+@indextable+'(nid) select id from '+@table+' order by id desc'
exec (@sql)
select @sql1='select * from '+@table+' O,'+@indextable+'t where O.id=t.nid and t.id>'+@PageLowerBound+' and t.id<='+@PageUpperBound+' order by t.id'
exec (@sql1)

22,207

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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