Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Multiple TOPs in Select and Subselct?

Author  Topic 

pwcphoto
Yak Posting Veteran

69 Posts

Posted - 2005-07-18 : 02:23:48
Is it ok to use Select TOP in a select statement and use Select Top in as Subsect within the same query?


From BOL
"The TOP clause applies to the single SELECT statement in which it is specified."

Does this mean Only one time in the entire query or just that select and not the subselect
as in the follwoing?

'SELECT ' + @strFields + ' FROM ' + @strTables + ' WHERE ' + @strPK + ' IN ' + '
(SELECT TOP ' + @strPageSize + ' ' + @strPK + ' FROM ' + @strTables +
' WHERE ' + @strPK + ' NOT IN ' + '
(SELECT TOP ' + @strSkippedRows + ' ' + @strPK + ' FROM ' + @strTables + @strFilterCriteria + @strGroupCriteria + @strSortCriteria + ') ' +

Thanks,

Phil

-----------------------
I used to be a rocket scientist. Now I'm just a space cadet...

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-07-18 : 02:39:11
Yes you can use

"The TOP clause applies to the single SELECT statement in which it is specified."

Select top 10 columns from table --Limit to 10 rows
Select columns from table--top not valid here

But setting rowcount will limit all the selects until it is reset to 0

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -