Parameterize the TOP clause in sql 2005

by admin 5. July 2008 23:26

SQL Server 2005 actually allows us to parameterize the TOP clause, using a variable, expression or statement. So you can do things like:

select top (@foo) [Column1] from [dbo].[myTable] 
 
select top (SELECT COUNT(*) FROM somewhere else) [Column1] from [dbo].[myTable]
 
select top (@foo + 5 * 4 / 2) [Column1] from [dbo].[myTable]

so we can create parameterized stored procedure with top as input parameter. In Sql 2005 it's simple:

create procedure dbo.getFoo 
    @top INT 
as 
begin 
    select top (@top) percent [column1]
        from [dbo].[myTable]         
end
go

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

SQL 2005

Comments are closed

Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen

Calendar

<<  March 2010  >>
MoTuWeThFrSaSu
22232425262728
1234567
891011121314
15161718192021
22232425262728
2930311234

View posts in large calendar