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 2005 Forums
 Transact-SQL (2005)
 top

Author  Topic 

shapper
Constraint Violating Yak Guru

450 Posts

Posted - 2008-02-26 : 17:27:13
Hello,

I am using a Numbers Table to create random data in a database:

declare @count int; set @count = 100
insert into dbo.Categories (CategoryID, [Name])
select top @count
newid(),
'Category ' + cast(n as nvarchar)
from @Numbers

I want to define the number of records but using a parameter.
Can't I do this?

I am getting an error.

Thanks,
Miguel

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2008-02-26 : 17:30:09
select top(@count) ...


_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
SSMS Add-in that does a few things: www.ssmstoolspack.com
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2008-02-26 : 17:31:55
As mentioned in one of your other posts on this topic, you can do this in several ways. Top is one way, but adding a WHERE clause will also work (and might be faster?) WHERE <= @Count
Go to Top of Page

jdaman
Constraint Violating Yak Guru

354 Posts

Posted - 2008-02-26 : 17:37:12
Or if you want the top n records by a specific order:
SELECT * FROM (
SELECT ROW_NUMBER() OVER(ORDER BY n) AS rn, *
FROM @Numbers ) a
WHERE rn <= @count
Go to Top of Page

shapper
Constraint Violating Yak Guru

450 Posts

Posted - 2008-02-26 : 17:59:46
Hi thanks,

I will use where if it is faster.

I don't need any order but thank you for the suggestion.

I created a numbers table but I needed to declare variables in the begin of my code to define the number of records to insert in each table.

By the way, how many records to you usually insert in test data?
Yesterday I got an error when trying to insert many records.
However, I think my code was not good ... I am improving it now with the help I got in SQLTeam Foruns.

Thank You,
Miguel
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-02-27 : 02:06:46
or use same ole stuff

SET ROWCOUNT @count
insert statement
SET ROWCOUNT 0


Madhivanan

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

- Advertisement -