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
 SQL Server Development (2000)
 Using the function InStr in queries

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2001-04-29 : 22:51:46
Felipe Amorim writes "I've found in a good ASP site the following code that should return a certain number of random records...
here's the code:

Dim objConn
Dim objRst
Dim strSQL
Dim strConnection
Dim str
Dim str1
Dim cnt
Dim cnt1
Dim rndMax
Dim RndNumber

strConnection="driver={Microsoft Access Driver (*.mdb)};dbq=" & server.mappath("testdb.mdb")
strSQL = "SELECT id FROM tblDates"

set objConn = Server.CreateObject("ADODB.Connection")
Set objRst = Server.CreateObject("ADODB.Recordset")
objConn.Open strConnection
set objRst.ActiveConnection = objConn
objRst.LockType = adLockOptimistic
objRst.CursorType = adOpenKeySet
objRst.Open strSQL

objRst.MoveLast
cnt = objRst.RecordCount
cnt1 = cnt
rndMax = cnt

If CInt(Request.Form("maxNumber")) < cnt Then
cnt1 = CInt(Request.Form("maxNumber"))
End If

str = ","
str1 = ","

Do Until cnt1 = 0
Randomize
RndNumber = Int(Rnd * rndMax)

If (InStr(1, str1, "," & RndNumber & "," ) = 0) Then
str1 = str1 & RndNumber & ","
cnt1 = cnt1 - 1
objRst.MoveFirst
objRst.Move RndNumber
str = str & objRst("id") & ","
End If

Loop

objRst.Close
Set objRst = Nothing

sql = "SELECT * FROM tblDates WHERE (((InStr(1,'" & str & "',(',' & [id] & ',')))<>0)) ORDER BY Rnd(IsNull([id])*0+1)"
Set objRst = Server.CreateObject("ADODB.Recordset")
set objRst.ActiveConnection = objConn
objRst.LockType = adLockOptimistic
objRst.CursorType = adOpenKeySet
objRst.Open sql

Content
objRSt.Close
set objRst = Nothing
objConn.CLose
Set objConn = Nothing
but in SQL 2000 this doesn't work, the InStr function is not reconizated....
does anyone nows another way?"
   

- Advertisement -