Programming :  Student Freelance Forum For Work Experience Builders' (CertificationPoint) The fastest message board... ever.
 
CLASSIC ASP-Find Out Whether a Specific Column Exists in a Database Table
Posted by: adcertpoint (Moderator)
Date: April 29, 2020 04:44PM

This snippet will allow you to check whether a certain column exists in a database table or not. Useful when you're coding against a database which schema you don't fully know. Just change the name of the column you're looking for (nameToCheck), the Connection String and the SQL SELECT statement, the bold items in this snippet.


<%
Dim oField ' As ADODB.Field
Dim oRecordset ' As ADODB.Recordset
Dim oConn ' As ADODB.Connection
Dim nameToCheck
Dim nameExists

' The column name you're looking for
nameToCheck = "ID"
nameExists = false

' Create connection
Set oConn = Server.CreateObject("ADODB.Connection"winking smiley
oConn.Open "YourConnectionString"

Set oRecordset = oConn.Execute("SELECT * FROM YourTable"winking smiley

For Each oField In oRecordset.Fields
If oField.Name = nameToCheck Then
nameExists = True
Exit For
End If
Next

oRecordset.Close()
oConn.Close()
Set oConn = Nothing
Set oRecordset = Nothing

Response.Write("Field " & nameToCheck & " found: " & nameExists)
%>

Options: ReplyQuote


Sorry, only registered users may post in this forum.
This forum powered by Phorum.