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
 Regarding SQL Queries

Author  Topic 

hotshot_21
Yak Posting Veteran

97 Posts

Posted - 2005-12-24 : 03:34:56
Iam developing a randomizing program .which selects random rows frm the database.iam using sql as backend and asp.net as front end.
i allow user to give a number say 1000, so 1000 random rows will be slected frm the database.iam using NEWID() for random selection.
But how do i put the user value into query. so as to select that many random rows from the databse.
please help me out on this

byrmol
Shed Building SQL Farmer

1591 Posts

Posted - 2005-12-24 : 04:44:28
set rowcount 1000

Look it up in the help file (BOL)

DavidM

Intelligent Design is NOT science.

A front-end is something that tries to violate a back-end.
Go to Top of Page

Srinika
Master Smack Fu Yak Hacker

1378 Posts

Posted - 2005-12-25 : 14:16:22
Create the query as follows
sSQL = "Select top " & txtRows.text & " * fromMyTbl order by NEWID() "
so that once created, the query looks like:
-- Select top 1000 * fromMyTbl order by NEWID()
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-12-26 : 00:07:47
If you use stored procedure pass the value and use Set Rowcount

Set RowCount @count
Select columns from yourTable order by Newid()
Set RowCount 0


Madhivanan

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

hotshot_21
Yak Posting Veteran

97 Posts

Posted - 2005-12-27 : 00:02:23
Thank u for responses.
Iam allowing user to enter some values integer into 3 textboxes,which i collect it and and depending on those value random numbers r slected from the database.Teh query is
"INSERT Randomeg11(UserID)SELECT TOP "&range1" "& range2 &" "&range3 &" UserID FROM Randomeg ORDER BY NEWID() "
Where range1,range2,range3 are number that user puts into text boxes. ihave tried using comma between "&range1&" , "&range2"& ,"&range3&" but it returns syyntax error near "&range3&".
please help me out
Go to Top of Page

shallu1_gupta
Constraint Violating Yak Guru

394 Posts

Posted - 2005-12-27 : 00:27:21
Hi Hotshot,
usage of Top Clause is
Selct Top <n> <column name(s)> from <Tablename>
where "n" is an integer value which can not be "," separated.

Go to Top of Page

Srinika
Master Smack Fu Yak Hacker

1378 Posts

Posted - 2005-12-27 : 09:24:38
Hi hotshot_21

What do u want to insert into the table?
Ur first request was to Select something from Database and hence the answers were for that.

What is there to be done with a range?
Give a few sample data, with ur selection criteria.

Do u want to create some Test UserIDs randomly ?
If that is the caseI think u can make use of generation of Random Numbers in the front end (in VB or ASP or Java ...)

Go to Top of Page

hotshot_21
Yak Posting Veteran

97 Posts

Posted - 2005-12-27 : 23:39:17
ok here is what iam doing
i hav large number of rows in single column,from which ihav to slect 4 or 5 number of rows randomly and iam inserting those rows into another table by using INSERT ,range1-2-3 is just the id of textboxes thati hav created in my .aspx page.and "UserID" is the name of the column in SQL tablenamed randomeg1 .Randomeg11 is the name of the table in which iam putting those randomly selected rows from table randomeg1.
so the problem is i am not able to collect all three value that user puts into textboxes.
Go to Top of Page

shallu1_gupta
Constraint Violating Yak Guru

394 Posts

Posted - 2005-12-28 : 00:13:17
you mean to say that in asp you are accepting 3 integer values(like 1,2,3) and you are inserting that many number of randomely selected rows in randomeg1 table.
you can insert the maximum of (integer values in the text boxes) into the table..
hopes this will solve ur problem..
INSERT Randomeg11(UserID)
SELECT TOP <(maximumvalue) or (sum of values in text box)> UserID FROM Randomeg ORDER BY NEWID()


Go to Top of Page
   

- Advertisement -