In Access, we can take one of our existing schema extractors and modify it slightly:
<% columnToFind = "foo" dbname = "/path_to.mdb" tablename = "tablename" ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;data source=" ConnStr = ConnStr & server.mappath(dbname) set adoxConn = CreateObject("ADOX.Catalog") set adodbConn = CreateObject("ADODB.Connection") adodbConn.open ConnStr adoxConn.activeConnection = adodbConn set table = adoxConn.Tables(tablename) found = false for each col in table.columns if lcase(col.name) = lcase(columnToFind) then found = true exit for end if next set table = nothing adodbConn.close: set adodbConn = nothing set adoxConn = nothing if found then response.write("Column exists.") else response.write("Column does not exist.") end if %> |
In SQL Server, you can use a much more direct approach:
<% columnToFind = "foo" dbname = "dbname" tablename = "tablename" connStr = "Provider=SQLOLEDB;Server=x.x.x.x;Database=" & _ dbname & ";UID=username;PWD=password" set conn = CreateObject("ADODB.Connection") conn.open connStr SQL = "SELECT COALESCE(COL_LENGTH('" & tablename & "'," & _ "'" & columnToFind & "'),0)" set rs = conn.execute(sql) if clng(rs(0))>0 then response.write("Column exists.") else response.write("Column does not exist.") end if rs.close: set rs = nothing conn.close: set conn = nothing %> |