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
 General SQL Server Forums
 New to SQL Server Programming
 Select Top Query

Author  Topic 

kalyformin
Starting Member

15 Posts

Posted - 2007-12-12 : 18:25:00
I have a stored procedure that takes in a parameter - @count.
Within thIS stored procedure I have a statement that selects the distinct Top "n" records.

How can I set the query to get something like this?

SELECT distinct top <@count>
FROM TABLE

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2007-12-12 : 18:34:17
What version of SQL Server are you using?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

kalyformin
Starting Member

15 Posts

Posted - 2007-12-12 : 18:40:51
SQL Server 2000
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2007-12-12 : 18:45:11
http://www.sqlteam.com/article/dynamic-sql-or-how-do-i-select-top-var-records

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

kalyformin
Starting Member

15 Posts

Posted - 2007-12-12 : 19:00:13
Thanks.

Is there anyway I can implement this with the following query-

INSERT <Table1>(col1,col2)
SELECT distinct TOP <@count>
col1,
col2
FROM <Table2>
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2007-12-12 : 19:04:01
Yes. Check out the article and then try the two solutions out.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/
Go to Top of Page
   

- Advertisement -