Script(s)

what I learn is what u c

Retrieving the First N Records from a SQL Query

leave a comment »

3 Techniques

1. Simple Fixed ‘N’

SELECT TOP 10 NewsURL, ArticleTitle, ArticleAuthor
FROM NewsStoriesTable
ORDER BY PublicationDate DESC

2. Dynamic ‘N’ Records

CREATE PROCEDURE getRecentArticles
(
@ResultCount int
)
AS

Declare @sSQL

Set @sSQL = “Select TOP ” + @ResultCount + ” NewsURL, ArticleTitle, ArticleAuthor
FROM NewsStoriesTable ORDER BY PublicationDate DESC”

sp_executesql @sSQL

3. Dynamic ‘N’ Records (My Preferred way)

CREATE PROCEDURE getRecentArticles
(
@ResultCount int
)
AS

SET ROWCOUNT @ResultCount — KEY LINE WHICH DOES THE MAGIC

SELECT NewsURL, ArticleTitle, ArticleAuthor
FROM NewsStoriesTable
ORDER BY PublicationDate DESC

Advertisements

Written by gchandra

May 16, 2006 at 3:29 pm

Posted in Database

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: