Sign in
|
Join
Home
Blogs
Ask
Just Learned
Interview Questions
Puzzles
Tutorials
Quiz
Modules
Getting Started with Adobe After Effects - Part 6:
Motion Blur
Syndicated Blogs
Upload Image
Close it
Select File
Its All About SQL Server
Like
Invite Friends
Browse by Tags ·
View All
Sql Server 2008
39
SQL Server 2012
39
Sql Server 2005
37
SQL Server Basics
11
TSQL Tips n Tricks
11
Performance Tuning
11
Sql Server Management
10
SQL Server Log
9
SQL Server Errors
9
Scripts
8
Archive ·
View All
July 2012
12
August 2012
8
September 2012
6
June 2012
6
November 2011
5
October 2011
3
August 2011
3
November 2012
2
October 2012
2
January 2012
2
ConnectSQL
Home
·
Most Liked
·
Most Discussed
·
Fans (16)
·
Preferences
SQL Server: Limit (MySQL) equal Function in SQL Server
Jul 11 2012 10:23AM
by
aasim abdullah
In MySQL, Limit is a powerful function used with SELECT query to return rows BUT within given range. We commonly need it when returning only required rows to application according to paging range. For example to return rows from 11 to 15 we will use following query in MySQL
SELECT
*
FROM
MyTable LIMIT 11
,
5
In SQL Server, same functionality can be achieved with three different ways.
1.
With Derived Table
(Most inefficient way, but applicable to all versions of SQL Server)
SELECT
*
FROM
(
SELECT
*,
ROW_NUMBER
()
OVER
(
ORDER
BY
ColumnName
)
AS
RowNum
FROM
MyTable
)
DerivedTable
WHERE
RowNum
>=
11
AND
RowNum
<=
11
+
(
5
-
1
)
2.
With CTE Common Table Expression
(Applicable to SQL Server 2005/2008/2012)
;
WITH
CTE
AS
(
SELECT
*,
ROW_NUMBER
()
OVER
(
ORDER
BY
ColumnName
)
AS
RowNum
FROM
MyTable
)
SELECT
*
FROM
CTE
WHERE
RowNum
>=
11
AND
RowNum
<=
11
+
(
5
-
1
)
3.
With OFFSET_ROW FETCH
(Applicable to SQL Server 2012 Only)
Here “
OFFSET
”
means, how many rows to skip, “
ROWS
FETCH
NEXT”
means,
how many rows to skip
SELECT
*
FROM
MyTable
OFFSET 10
ROWS
FETCH
NEXT
5
ROWS
ONLY
;
Previous:
SQL Server: How Local Variables Can Reduce Query Performance
Next:
SQL Server: Script to Generate HTML Report/mail for Databses Current Size, Growth Rate and Available Disk Space
Republished from
Connect SQL
[12 clicks]
. Read the original version
here
[2 clicks]
.
aasim abdullah
23 ·
7% ·
2372
1
Liked
0
Lifesaver
0
Refreshed
0
Learned
0
Incorrect
Like this
Post this to my facebook wall
Tweet this
This was a true life saver
Thanks, this helped me to refresh my memory
Very informative, I just learned something new
I disagree, this information is incorrect
Submit
Your Comment
Sign Up
or
Login
to post a comment.
"SQL Server: Limit (MySQL) equal Function in SQL Server"
rated 5 out of 5 by 1 readers
SQL Server: Limit (MySQL) equal Function in SQL Server
,
5.0
out of
5
based on
1
ratings
+aasim abdullah
Copyright © Rivera Informatic Private Ltd
Contact us
Privacy Policy
Terms of use
Report Abuse
Advertising
[ZULU1097]