帮忙看看这一段存储过程的代码,老是报错。
ELSE IF(@topnum> 0)
BEGIN
IF(@type> 0)
SELECT TOP (@topnum)
A.ArticleID,
A.ArticleTitle,
A.ArticleContent,
A.ArticleImg,
PublicDate,
ViewTimes,
A.ArticleType As TypeID,
B.TypeTxt
FROM ArticleInfo A
JOIN TypeInfo B
ON A.ArticleType=B.TypeID
AND A.ArticleType=@type
ORDER BY ArticleID DESC
END
Incorrect syntax near '( '.SELECT TOP (@topnum)指的是这一行
------解决方案--------------------TOP后只允许使用常数,不能使用变量。
可以使用动态SQL或SET ROWCOUNT
------解决方案--------------------delcare @sql varchar(300)
set @sql= 'SELECT TOP '+ (@topnum)+ '
A.ArticleID,
A.ArticleTitle,
A.ArticleContent,
A.ArticleImg,
PublicDate,
ViewTimes,
A.ArticleType As TypeID,
B.TypeTxt
FROM ArticleInfo A
JOIN TypeInfo B
ON A.ArticleType=B.TypeID
AND A.ArticleType=@type
ORDER BY ArticleID DESC '
exec (@sql)
------解决方案--------------------ELSE IF(@topnum> 0)
BEGIN
IF(@type> 0)
exec( 'SELECT TOP '+@topnum+ ' A.ArticleID,A.ArticleTitle,A.ArticleContent,A.ArticleImg,PublicDate,ViewTimes,A.ArticleType As TypeID,B.TypeTxt FROM ArticleInfo A JOIN TypeInfo B ON A.ArticleType=B.TypeID AND A.ArticleType= '+@type+ ' ORDER BY ArticleID DESC ')
END