<% Sub GetQuote(byVal strQuote, byval strAuthor) Dim intMaxID& Dim intRecordID dim strSQL& Dim oConn& Dim oRS set oConn = Server.CreateObject("ADODB.Connection") oConn.Open "Database=mydb;DSN=Quotes;UID=sa;Password=;" strSQL = "SELECT MaxID=max(QuoteId) from Quotes" Set oRS = oConn.Execute(strSQL) If oRS.EOF Then strQuote = "站长太懒了,今天没有更新内容." strAuthor = "呵呵" Exit Sub Else intMaxID = oRS("MaxID") End If Randomize intRecordID= Int(Rnd * intMaxID) + 1 strSQL = "Select * from quotes where QuoteID=" & intRecordID & ";" Set oRS = oConn.Execute(strSQL) If oRS.EOF Then strQuote = "站长太懒了,今天没有更新内容." strAuthor = "呵呵" Exit Sub Else oRS.MoveFirst strQuote = oRS("Quote") strAuthor = oRS("Author") End If oRS.Close oConn.Close Set oRS = Nothing set oConn = Nothing End Sub %>
其实在程序中如果使用一个嵌套的SQL能够提高性能,例如这样
Select * from Quotes where QuoteID = (Select int ( RND * Max(QuoteID) ) from Quotes );