日期:2014-05-18  浏览次数:20574 次

兄弟们帮帮忙来,select top 后跟个变量名怎么不行呢,访问多少条的,不知该如何写
select   top 后跟个变量名怎么不行呢,访问多少条的,不知该如何写,如下,报错

declare   @top   int
set     @top=10
SELECT   top   @to   CustomerID,CompanyName   FROM   Customers

------解决方案--------------------
try

CREATE PROCEDURE Top_N_News
@ID integer, @TopN integer, @PicN integer
AS
Declare @IDPath varchar(4000)
Declare @TopN1 varchar(20)
Set @TopN1 = @TopN
Select @IDPath=(IDPath+ '% ') From H_new_cate Where ID=@ID

If @PicN > 0
Begin
EXEC( 'Select Top ' + @TopN1 + ' news_id,news_title,news_pic From H_news Where news_cate In (Select ID From H_new_cate Where IDPath Like ' ' ' + @IDPath + ' ' ') Order By news_id Desc ')
End
Else
print 'fsdfsdfdsf '
GO
------解决方案--------------------
可以这样

declare @top int
set @top=10
set Rowcount @top
SELECT CustomerID,CompanyName FROM Customers