Find a matching SQL Server record safely

Use this example to see the core idea, then work through the complete steps below. Code snippets are Classic ASP / VBScript or SQL Server T-SQL as labelled, not PHP.

Find a row using a query parameter

VBScript example

Set cmd = Server.CreateObject("ADODB.Command")
Set cmd.ActiveConnection = cn
cmd.CommandType = 1 ' adCmdText
cmd.CommandText = "SELECT emp_no, name FROM emp_m WHERE emp_no = ?"
cmd.Parameters.Append cmd.CreateParameter("pEmp", 3, 1, , 145)
Set rs = cmd.Execute
If rs.EOF Then Response.Write "No matching record"

This example assumes an open SQL Server ADO connection cn. Bind an integer with adInteger = 3; never concatenate visitor input into query text.

On this page — Show page sectionsHide page sections

To test whether a record exists, run a query for the key and then check the returned Recordset's EOF property. When the key comes from the request, pass it as an ADO parameter rather than concatenating it into SQL.

Parameterized query

SELECT emp_no, name FROM emp_m WHERE emp_no = ?

Complete Classic ASP example

Dim conn, cmd, rs, empNo
empNo = Trim(Request.QueryString("emp_no"))

Set conn = Server.CreateObject("ADODB.Connection")
conn.ConnectionString = aConnectionString
conn.Open
Set cmd = Server.CreateObject("ADODB.Command")
Set cmd.ActiveConnection = conn
cmd.CommandText = "SELECT emp_no, name FROM emp_m WHERE emp_no = ?"
cmd.CommandType = adCmdText
cmd.Parameters.Append cmd.CreateParameter("pEmpNo", adInteger, adParamInput, , CInt(empNo))
Set rs = cmd.Execute
If rs.EOF Then
Response.Write "No matching record"
Else
Response.Write "Name: " & Server.HTMLEncode(CStr(rs("name")))
End If
rs.Close
Set rs = Nothing
Set cmd = Nothing
conn.Close
Set conn = Nothing

This example assumes adovbs.inc is included so constants such as adInteger, adParamInput and adCmdText are available. Validate that the request value is numeric before calling CInt().

Validate request values before querying

When looking up an integer key, verify that the submitted value is numeric and in the expected range before calling CLng(). ADO parameters protect the SQL structure but do not replace input validation.

No matching rows

A successful query can return an empty Recordset. Check rs.EOF before reading rs("name"); distinguish “no match” from an actual database connection error.

Frequently asked questions

Do these examples require Classic ASP on IIS?
Yes. The server-side code uses VBScript and ADO components from Classic ASP; the .php file here is the Plus2Net tutorial wrapper.
Can I pass form input directly into SQL strings?
No. Validate it and bind it through ADO parameters rather than concatenating it into SQL text.
What should I do if no records match?
Check EOF before accessing fields and show a clear empty-result message.

Back to top ↑


ASP Home






✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer