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
 Transact-SQL (2000)
 Random query

Author  Topic 

oepirobo
Starting Member

36 Posts

Posted - 2003-01-24 : 08:30:34
I need a statement to get a random record from a table.

Something like this: Select random top 1 * from table1.

nr
SQLTeam MVY

12543 Posts

Posted - 2003-01-24 : 08:33:40
Select random top 1 * from table1 order by new_id()


==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

mohdowais
Sheikh of Yak Knowledge

1456 Posts

Posted - 2003-01-24 : 13:43:16
quote:

Select random top 1 * from table1 order by new_id()



There is no RANDOM supposed to be there, is there?

OS

Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2003-01-25 : 11:23:47
quote:

Select random top 1 * from table1 order by new_id()



oops, ty.
Select top 1 * from table1 order by new_id()


==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

ValterBorges
Master Smack Fu Yak Hacker

1429 Posts

Posted - 2003-01-25 : 11:43:20
This does not work on SQL under Windows NT 4.0. as of sp2 don't know if sp3 fixes that.



Go to Top of Page

oepirobo
Starting Member

36 Posts

Posted - 2003-01-27 : 06:57:36
I've got SQLServer 2000. This didn't work, it says:
Server: Msg 195, Level 15, State 10, Line 1
'new_id' is not a recognized function name.

Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2003-01-27 : 07:57:36
Take out the underscore:

Select top 1 * from table1 order by newid()

Go to Top of Page

oepirobo
Starting Member

36 Posts

Posted - 2003-01-27 : 08:25:56
It works, Thanks.

Go to Top of Page
   

- Advertisement -