首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

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

2012-02-11 
兄弟们帮帮忙来,select top 后跟个变量名怎么不行呢,访问多少条的,不知该如何写selecttop 后跟个变量名怎

兄弟们帮帮忙来,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


热点排行