sp_executesql 与表变量

arjsyy arjsyy 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
CREATE PROC sp_FenYe_news
@startIndex int,
@endIndex int,
@where nvarchar(1000),
@count int output
AS
SET NOCOUNT ON
DECLARE @indextable table([id] int identity(1,1),nid int)--声明一个表变量
DECLARE @sql AS NVARCHAR(4000)
SET ROWCOUNT @endIndex
IF(LEN(@where)>0 AND @where is not null)--判断条件是不是为空
BEGIN
SET @sql='INSERT INTO @table (nid) SELECT [id] FROM news WHERE'+ @where
EXEC SP_EXECUTESQL @sql,N'@where',@where
END
ELSE
INSERT INTO @indextable(nid) SELECT [id] FROM news
SELECT @count=COUNT([id]) from @indextable
SELECT * FROM news n
INNER JOIN @indextable t ON n.[id]=t.nid
WHERE t.[id] between @startIndex and @endIndex ORDER BY t.[id] DESC
SET NOCOUNT OFF


这是一个分页的存储过程 ,现在我只要一执行就出现@indextable没有定义,
我现在知道是因为@indextable是一个表变量的原因,搜了一些方法仍然无解,请大侠帮助一下小妹
...全文
评论 15
收藏 已收藏
分享
返回顶部
arjsyy
MS-SQL Server 34,877 成员 254,635 内容