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.
| 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 = 100insert into dbo.Categories (CategoryID, [Name])select top @count newid(), 'Category ' + cast(n as nvarchar)from @NumbersI 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 1980blog: http://weblogs.sqlteam.com/mladenpSSMS Add-in that does a few things: www.ssmstoolspack.com |
 |
|
|
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 |
 |
|
|
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 ) aWHERE rn <= @count |
 |
|
|
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 |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-02-27 : 02:06:46
|
| or use same ole stuffSET ROWCOUNT @countinsert statementSET ROWCOUNT 0MadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|