Get The No. Of Columns!

A

Arpan

How do I find out how many columns exist in a SQL Server DB table
before displaying the records of that table?

Also how do I get all the column names as an array so that I can
seperate each column using the Split function?

Thanks,

Arpan
 
C

Chris Hohmann

Arpan said:
How do I find out how many columns exist in a SQL Server DB table
before displaying the records of that table?

Also how do I get all the column names as an array so that I can
seperate each column using the Split function?

Thanks,

Arpan

Use the Information_Schema.Columns system view to get the names of the
columns for a given table:

SELECT column_name
FROM Information_Schema.Columns
WHERE table_name = 'MyTable'

You can then use the GetRows method of the ADODB.Recordset object to
retrieve the column names into a two (2) dimensional array. Here's a link to
the online documentation for the GetRows method:
http://msdn.microsoft.com/library/en-us/ado270/htm/mdmthgetrows.asp
 
A

Arpan

Thanks Chris for your help. It has served my purpose.

Thanks once again.

Regards,

Arpan
 
E

Egbert Nierop \(MVP for IIS\)

Arpan said:
How do I find out how many columns exist in a SQL Server DB table
before displaying the records of that table?

Also how do I get all the column names as an array so that I can
seperate each column using the Split function?

Dim rs As Recordset
Set rs = New Recordset
rs.MaxRecords = 1
rs.Open "yourtable", [yourconn], adOpenForwardOnly
Dim fld As Field
Dim fldCount As Long
lng = rs.Fields.Count
ReDim fldARray(lng - 1) As String

Dim cx As Long
For cx = 0 To lng - 1
fldARray(cx) = rs.Fields.Item(cx).Name
Next
 
A

Arpan

Chris I have another question regarding the GetRows() method. How do I
get the column names using GetRows()? I think that isn't possible,
isn't it?

Thanks once again & thanks to Egbert too

Regards,

Arpan
 
C

Chris Hohmann

Arpan said:
Chris I have another question regarding the GetRows() method. How do I
get the column names using GetRows()? I think that isn't possible,
isn't it?

What I was suggesting was to use the query on the Information_Schema.Columns
view in _conjunction_ with the GetRows method of the ADODB.Recordset object.
The resulting two (2) dimensional array will contain the names of the
columns in the table you specified in the query.
 
A

Arpan

That's exactly what I did but if I am not wrong, the resulting
2-dimensional array will contain the NUMBER of columns in the table (&
not the NAMES of the columns) & the number of records in the table,
isn't it?

Please do correct me if I am wrong.

Thanks once again,

Regards,

Arpan
 
E

Evertjan.

Arpan wrote on 08 sep 2005 in microsoft.public.inetserver.asp.general:
That's exactly what I did but if I am not wrong, the resulting
2-dimensional array will contain the NUMBER of columns in the table (&
not the NAMES of the columns) & the number of records in the table,
isn't it?

Don't know about SQL-server, I use Microsoft.Jet.OLEDB.4.0.

I use this:

<%@Language=VBScript%>
<!-- #include file ="openDbaseAfterPasswordProtection.asp"-->

<body style='background-color:navy;padding:50px;'>
<div style='padding:30px;background-color:white;font-
size:14pt;width:350px;'>

<%
dim columnTypes(205)
columnTypes(0) = "Empty"
columnTypes(2) = "SmallInt"
columnTypes(3) = "Integer"
columnTypes(4) = "Real"
columnTypes(5) = "Double"
columnTypes(6) = "Currency"
columnTypes(7) = "Date"
columnTypes(8) = "BSTR"
columnTypes(9) = "IDispatch"
columnTypes(10) = "Error Code"
columnTypes(11) = "Boolean"
columnTypes(12) = "Variant"
columnTypes(13) = "IUnknown"
columnTypes(14) = "Decimal"
columnTypes(16) = "TinyInt"
columnTypes(17) = "Unsigned TinyInt (BYTE)"
columnTypes(18) = "Unsigned Small Int (WORD)"
columnTypes(19) = "Unsigned Int (DWORD)"
columnTypes(20) = "BigInt"
columnTypes(21) = "Unsigned Big Int"
columnTypes(64) = "FileTime"
columnTypes(72) = "Unique Identifier (GUID)"
columnTypes(128) = "Binary"
columnTypes(129) = "Char"
columnTypes(130) = "nChar"
columnTypes(131) = "Numeric"
columnTypes(132) = "User Defined (UDT)"
columnTypes(133) = "DBDate"
columnTypes(134) = "DBTime"
columnTypes(135) = "SmallDateTime"
columnTypes(136) = "Chapter"
columnTypes(138) = "Automation (PropVariant)"
columnTypes(139) = "VarNumeric"
columnTypes(200) = "VarChar"
columnTypes(201) = "Text"
columnTypes(202) = "nVarChar"
columnTypes(203) = "nText"
columnTypes(204) = "VarBinary"
columnTypes(205) = "Image"


function tables(tablename)
Response.Write "<hr><hr>"&tablename&"<hr>"
set rs = CONNECT.Execute("select * from " & tablename)
for each column in rs.fields
Response.Write column.name & " [" & columnTypes(column.type)
ct = column.type
if ct = 129 or ct = 130 or ct = 200 or ct = 202 then
' definedSize only works in SQL Server
Response.write " (" & column.definedSize & ")"
end if
Response.Write "]"
if column.Properties("IsAutoIncrement") then
response.write " (AutoNumber)"
end if
Response.Write "<br>"
next
rs.close: set rs = nothing
end function

tables("tblNumber1") ' name your tables

tables("tblNumber2")

' etc.

CONNECT.close: set CONNECT = nothing
%>
</div>

I forgot where the souce was from.
 
C

Chris Hohmann

Arpan said:
That's exactly what I did but if I am not wrong, the resulting
2-dimensional array will contain the NUMBER of columns in the table (&
not the NAMES of the columns) & the number of records in the table,
isn't it?

Please do correct me if I am wrong.

It's very difficult to diagnose your problem since I don't have any idea
what your code looks like. Please post your code, the output of that code
and the desired results.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top