Friday, December 14, 2007

How to get the first record by using SQL?

How to get the first record by using SQL?

Sometimes, we only need to use the first record. However, normally what we do is to query all records and use the first one only.

Now we can use the SQL command to query the first record only:


<%

Set conn = Server.CreateObject("Adodb.Connection")

conn.Open "Driver={Microsoft Access Driver (*.mdb)};DBQ=" & Server.MapPath("db1.mdb")

Set rs = Server.CreateObject("Adodb.Recordset")

strSQL ="SELECT First(Price) As FirstRecortd FROM Stock;" 'This SQL we only return a single records (first record)

rs.Open strSQL,conn,1,3

If Not rs.EOF Then

Response.Write("The first record is " & rs.fields("FirstRecortd") & "
")

End If

rs.Close

Set rs = Nothing

%>

Or, we can get the last record:

SELECT Last(Price) As FirstRecortd FROM Stock;

No comments: