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
 SQL Server Development (2000)
 Writing to Stored Procedures

Author  Topic 

Ferox
Starting Member

18 Posts

Posted - 2005-05-30 : 07:11:21
Is it possible to write out to a Stored Procedure in this format, where I am passing in a parameter of 10 or 20 etc so I can pull out the top 10 or 20:

Create Stored Procedure spsh_Test
(
@nText INT
)

Select
TOP @nText myField
FROM
myTable
GO

Merkin
Funky Drop Bear Fearing SQL Dude!

4970 Posts

Posted - 2005-05-30 : 07:18:00
TOP doesn't accept a variable yet, it will in SQL 2005.

Until then, we can use rowcount.

SET ROWCOUNT @nText

SELECT myField FROM myTable

SET ROWCOUNT 0



Damian
Ita erat quando hic adveni.
Go to Top of Page

Ferox
Starting Member

18 Posts

Posted - 2005-05-30 : 07:28:30
Thanks Merkin, thats exactly what I was looking for! :)
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-05-30 : 08:03:34
quote:
TOP doesn't accept a variable yet, it will in SQL 2005


I think top can accept variable if we use Dynamic SQL

Create Procedure Returntop (@topno integer)
as
Create table #t(i integer identity,n char(10))
insert into #t(n) values('test1')
insert into #t(n) values('test2')
insert into #t(n) values('test3')
insert into #t(n) values('test4')
insert into #t(n) values('test5')
insert into #t(n) values('test6')
insert into #t(n) values('test7')
insert into #t(n) values('test8')
insert into #t(n) values('test9')
insert into #t(n) values('test10')
Exec('select top '+@topno +' * from #t')

Run
ReturnTop 5 -- will return top 5
ReturnTop 7 -- will return top 7



Madhivanan

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

Merkin
Funky Drop Bear Fearing SQL Dude!

4970 Posts

Posted - 2005-05-30 : 08:09:00
quote:

I think top can accept variable if we use Dynamic SQL



Yes, but
1. That isn't top accepting a variable param, it's executing a string
2. It's silly, we've already solved that problem.



Damian
Ita erat quando hic adveni.
Go to Top of Page
   

- Advertisement -