存储过程传表名为参数

想起你的日子 2008-11-18 03:58:15
create procedure [dbo].[P_GetPaged]
(@startIndex int,
@endIndex int
)
as
begin
set nocount on
declare @indextable table(id int identity(1,1),nid int)
set rowcount @endIndex
insert into @indextable(nid) select Windex from T_Wares order by Windex desc

select w.* from t_wares w left outer join @indextable t on w.windex=t.nid
where t.id between @startIndex and @endIndex order by t.id
set nocount off
end
本人写了以上这个存储过程来实现分页,想将表T_Wares 及列Windex 也作为参数传入
自己写了下,但总是运行不过,请大家指点一二(以下是在下写的)


create proc p_gettableinfo
(
@tablename nvarchar(100),
@colname nvarchar(100),
@startIndex int,
@endIndex int
)
as
begin
declare @strsql varchar(8000)
declare @indextable table(id int identity(1,1),nid int)
set @strsql=' into '+@indextable+'(nid) select '+@colname+' from '+@tablename +' order by '+@colname+' desc'
exec (@strsql)
set @strsql=' Select w.* from +'@indextable'+ w left outer join '+@indextable'+ t on t.nid='+@tablename+'.'+@colname
set @strsql=@strsql+' where t.id between '+@startIndex+' and '+@endIndex+' order by t.id desc '
exec (@strsql)
end

希望大家帮帮忙
...全文
38 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
水族杰纶 2008-11-18
  • 打赏
  • 举报
回复
declare @indextable table(id int identity(1,1),nid int)
表變量不能放在動態中

22,209

社区成员

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

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