34,588
社区成员
发帖
与我相关
我的任务
分享
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
alter PROCEDURE [dbo].[Song_getForHtml_sp]
@m int,
@n int
AS
BEGIN
SET NOCOUNT ON;
select top @n-@m+1 songid
from (select top @n songid from song order by songid asc) a
order by songid desc
END
GO
--sql 2005
create PROCEDURE [dbo].[Song_getForHtml_sp]
@m int,
@n int
AS
BEGIN
SET NOCOUNT ON;
select top ( @n-@m+1 ) songid
from (select top ( @n ) songid from song order by songid asc) a
order by songid desc
END
GO
--sql 2000
alter PROCEDURE [dbo].[Song_getForHtml_sp]
@m int,
@n int
AS
BEGIN
SET NOCOUNT ON;
declare @sql nvarchar(2000)
set @sql='
select top ( '+ltrim(@n)+'-'+ltrim(@m)+'+1 ) songid
from (select top ( '+ltrim(@n)+' ) songid from song order by songid asc) a
order by songid desc '
exec (@sql)
END
GO
/*
在TOP后面使用变量
(爱新觉罗.毓华 2008-01-02 广东深圳)
*/
--SQL SERVER 2005 的写法
use adventureworks
go
DECLARE @Percentage int
SET @Percentage = 1
SELECT TOP (@Percentage) PERCENT
Name
FROM Production.Product
ORDER BY Name
/*
Name
----------------------
Adjustable Race
All-Purpose Bike Stand
AWC Logo Cap
BB Ball Bearing
Bearing Ball
Bike Wash - Dissolver
(6 行受影响)
*/
-----------------------------------
--SQL SERVER 2000 的写法
create table a([id] [int])
insert into a(id) values(1)
insert into a(id) values(2)
insert into a(id) values(3)
insert into a(id) values(4)
insert into a(id) values(5)
declare @num as int
declare @sql as varchar(2000)
set @num = 2
set @sql = 'select top ' + cast(@num as char) + ' * from a'
exec(@sql)
drop table a
/*
id
-----------
1
2
*/