RandomRecords

Returns random IDs (or any column) from SQL Database table, passing number of records to return, column to return, table name and where condition.
Returns list separated by {{$R$}}

NEWID() works in MSSQL, replace NEWID() with RAND() to use same query for MySQL


Public

Tested

My Own Work
Function RandomRecords(Records, Column1, Table1, Where1)
 ' MSSQL = SELECT Top(10) ID FROM Anmars_User.IQNames_Ar Where Order_1=2 ORDER BY NEWID()
 ' MySQL = Select * from Coupons where Status='" & Status_Active & "' order by RAND()
 RandomRecords = ""
 fSQL="Select Top(" & Records & ") " & Column1 & " from " & Table1 & " " & Where1 & " order by NEWID()"
 ' fSQL="Select Top(" & Records & ") " & Column1 & " from " & Table1 & " " & Where1 & " order by RAND()"
 set recF=connection.execute(fSQL)
 Do until recF.Eof
  GGe=GGe & trim(recF(Column1)) & "{{$R$}}"
  recF.MoveNext
 Loop
 RandomRecords = GGe
End Function

Records, Column1, Table1, Where1

Reecs = RandomRecords(10, "ID", "BlogPosts", " where status = 1")

Views 3755 Downloads 1085

Classic ASP Database
ANmarAmdeen
714
Attachments
Revisions

v2.0