Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Qeury MSSQL database and get the field names

Status
Not open for further replies.

skurpyun

Programmer
Jun 19, 2002
60
US
If this is not the correct forum to post this question in, forgive me.

Here is my problem. I want to query an MsSQL database and manipulate the data returned. When this data is printed to the screen, i need to match it up with its respective field (column) name. The catch is, the table's column names will change regularly in the near future.

So basically, what I want to find out is, how do i query a database and find out the field (column) names of the data returned?

-sk
 
>> The catch is, the table's column names will change
>> regularly in the near future.

That's just not right [bugeyed]

Anyway, look at the ADODB SDK it's something like
Code:
Recordset.fields(n).name


-pete
 
Ok, my apologies, after playing around with my code i figured out how to do it. basically, I query the database, get the resultset, then I loop through the field collection. For each field in the field collection, i simply print it's name. So simply i had to laugh at myself for not knowing this. Here's the code:

Code:
sql = "select * from myDatabase where id=4"

set rs = connectionobj.execute(sql)

if not rs.eof

  for each fieldobj in rs.fields
     response.write fieldobj.name & &quot;<br>&quot; 'print the field name.
  next   

end if

rs.close
set rs = nothing


note: i left out all the database connection stuff, etc.

if you read this to offer your help, thanks. And if you have a better way of doing this, by all means let me know.
 
Palbano,

I understand your concern. I have the same concerns. But the table is basically to track various sites owned by a client. They are continually adding sites. And working with the database schema i inherited, I had to figure out a way to get the field names, in case a new site is added. At a later point I intend to redo the database in a more efficient way. But that requires time that I nor the client have right now!

-sk
 
if not RS.eof then
for each field in rs.fields
response.write &quot;stuff&quot; & field.name & &quot;more stuff&quot; ' use this for your ... say table headers
Next
do while not rs.eof
for each field in rs.fields
response.write &quot;stuff&quot; & RS(field.name) & &quot;more stuff&quot; ' use this for outputting the values of the ever changing field names
Next
RS.MoveNext
Loop
End if
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top