PDA

View Full Version : NewID()



pucca
April 18th, 2006, 06:59 AM
I want to random select 5 records from my database.. I got this code,

Code:

SELECT Top 5 * FROM comptbl ORDER BY NewID()



but its gives me an error saying "Undefined function 'NewID() in expression."
What must I do? The site is done in asp.. with a access database - if that helps?..

thanks for the help! :puzzled:

λ
April 18th, 2006, 07:52 AM
Please don't double post. Thanks.

I've never seen syntax like that in a SQL query..

teiz77
April 19th, 2006, 03:16 AM
I want to random select 5 records from my database.. I got this code,

Code:

SELECT Top 5 * FROM comptbl ORDER BY NewID()



but its gives me an error saying "Undefined function 'NewID() in expression."
What must I do? The site is done in asp.. with a access database - if that helps?..

thanks for the help! :puzzled:
NewId() only works on SQL server...

Try something like this:


<%
Randomize()
randNum = (CInt(1000 * Rnd) + 1) * -1

set conn = CreateObject("ADODB.Connection")

sql = "SELECT TOP 5 cols," & _
"r = Rnd(" & randNum & ")" & _
"FROM TableName " & _
"ORDER BY r"

set rs = conn.execute(sql)

response.write rs(0)

' ...
rs.close: set rs = nothing
conn.close: set conn = nothing
%>