Congratulations to all the winners of April 2013, they have won INR 3400 cash and INR 20147 worth prizes !
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 4725 |  Welcome, Guest!   Register  Login
Home > Articles > Sql Server > Difference between TOP clause in SQL 2000 and SQL 2005

Difference between TOP clause in SQL 2000 and SQL 2005

Article posted by Chinnu21 on 4/23/2008 | Views: 10875 | Category: Sql Server | Level: Intermediate red flag


Generally TOP clause is used to perform SELECT on top n results. This feature of TOP is extended in SQL 2005 so that we can also use expression apart from int, bigint and percent to perform query and also extended to be used in UPDATE and DELETE statements.

Introduction


Generally TOP clause is used to perform SELECT on top n results. This feature of TOP is extended in SQL 2005 so that we can also use expression apart from int, bigint and percent to perform query and also extended to be used in UPDATE, and DELETE statements.

In SQL 2000


syntax: select Top N [Percent]

EX:

select Top 10 * from TableName
or
select Top 10 Percent * from TableName

n specifies how many rows are returned. If PERCENT is not specified, n is the number of rows to return. If PERCENT is specified, n is the percentage of the result set rows to return

Drawbacks:


1) We could not parameterize.
2) It will work only for select statements.

If we want to restrict the number of rows affected by a select at runtime, or restrict the rows affected by an update or delete you had to explicitly describe the rows using a join or where clause, or you could cheat a bit by using ROWCOUNT, like this

set rowcount 10
delete from table where payratefieldname=1
set rowcount 0

It will work but the risk is if for some reason rowcount is not set to 0 then the other statements will also restricted to 10.

All these drawbacks are overcome in SQL 2005 by introducing Expression in syntax.

In SQL 2005



syntax: select Top (Expression) [Percent]

EX:

Is the numeric expression that specifies the number of rows to be returned. expression is implicitly converted to a float value if PERCENT is specified; otherwise, it is converted to bigint.

Ex:

select Top 10 * from TableName
or
select Top 10 Percent * from TableName
or we can set at runtime as

Declare @ int
SET @topNum = 10
select TOP (@topNum) * from

For select statements you must specify parentheses if you are passing a parameter, otherwise they are optional

Select Top (@topNum) * from TableName

Select Top 10 * from TableName

When doing an update or delete, you have to use the parentheses in both cases:

Delete Top (@topNum) from employeesDelete Top (10) from TableName

update Top (@topNum) TableName set fieldname = @fieldvalue

update Top (10) from employees set fieldname = @fieldvalue


Conclusion


This is nice tricky change introduced in SQL 2005

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

Experience:2 year(s)
Home page:http://anil.myfunda.net
Member since:Sunday, April 27, 2008
Level:Starter
Status: [Member]
Biography:
>> Write Response - Respond to this post and get points
Related Posts

DBAs often need to track the changes being made to the database objects such as tables, user-defined functions and stored procedures etc.,. Our team size was 10 and a couple of members were responsible for each of the modules. My requirement was to track the changes made to the stored procedures right from its creation. My earlier articles explained on how source control can be made available for new/existing stored procedures. If you have missed those articles please catch them up at, http://dotnetfunda.com/articles.

This is the part 1 of series of article on SSIS

This is part 36 of the series of article on SSIS. In this article we are going to see on how to use an Aggregate (Maximum) data flow transformation control in SSIS packaging.

This is part 34 of the series of article on SSIS. In this article we are going to see on how to use an Aggregate (COUNT) data flow transformation control in SSIS packaging.

Creating the proper index can drastically increase the performance of an application. In this article we will learn how to create indexes in SQL Server database.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/19/2013 11:35:56 PM