SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 Script Library
 Dynamic Array
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

savvy95
Starting Member

23 Posts

Posted - 01/03/2007 :  09:04:47  Show Profile  Reply with Quote
Please excuse me if this is the wrong forum. I hope your combined genius will be able to point me in the right direction.

I have an ASP page that accepts any SQL query. I want to create a table based on the result set so the number of columns can grow/shrink. But I don't know how. Can anyone help me?

My ASP Code shows how I create a table with only 8 fields.

<%
Dim objConn
dim SQLstmt
Dim objRS
Dim SQLqry
Dim UID
Dim RecordArray
Dim I

UID = right(Request.ServerVariables("AUTH_USER"),8)
SQLqry = Request.Form("qry")

Set objConn = Server.CreateObject("ADODB.Connection")
objConn.ConnectionString="Trusted_Connection=yes;driver=SQL Server;server=Server;database=db;"
objConn.Open 


If SQLqry = "" then
	Response.Write "Please Enter a SQL Query"
	Else

Set objRS = objConn.Execute(SQLqry)
         Response.Write "<H5 align = ""center"">" & sqlqry & "</H5>"

Response.Write "<Table align = ""center"" border = ""1"">"
For I = 0 To objRS.Fields.Count - 1
  Response.Write "<TD><B>" & objRS(I).Name & "</B></TD>"
Next


	Do while not objRS.EOF 
	  RecordArray = objRS.GetRows(30)

	  For I = 0 To UBound(RecordArray,2)
		Response.Write "<TR>"
		Response.Write "<TD>" & RecordArray(0,I) & "</TD>"
		Response.Write "<TD>" & RecordArray(1,I) & "</TD>"
		Response.Write "<TD>" & RecordArray(2,I) & "</TD>"
		Response.Write "<TD>" & RecordArray(3,I) & "</TD>"
		Response.Write "<TD>" & RecordArray(4,I) & "</TD>"
		Response.Write "<TD>" & RecordArray(5,I) & "</TD>"
		Response.Write "<TD>" & RecordArray(6,I) & "</TD>"
		Response.Write "<TD>" & RecordArray(7,I) & "</TD>"
		Response.Write "</TR>"
	  Next
	Loop
            Response.Write "</TABLE>"
	objRS.close
	objConn.close
	set objRS=nothing
	set objConn=nothing

End if
%>

harsh_athalye
Flowing Fount of Yak Knowledge

India
5509 Posts

Posted - 01/03/2007 :  09:24:52  Show Profile  Visit harsh_athalye's Homepage  Click to see harsh_athalye's MSN Messenger address  Send harsh_athalye a Yahoo! Message  Reply with Quote
By TABLE you mean HTML Table element or physical table in the back-end?

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

jsmith8858
Dr. Cross Join

USA
7423 Posts

Posted - 01/03/2007 :  10:53:16  Show Profile  Visit jsmith8858's Homepage  Reply with Quote
you need two nested loops:

	  For I = 0 To UBound(RecordArray,2)
            Response.Write "<TR>"
            For C = 0 to Ubound(RecordArray,1)
		Response.Write "<TD>" & RecordArray(C,I) & "</TD>"
            Next
            Response.Write "</TR>"
	  Next

- Jeff

Edited by - jsmith8858 on 01/03/2007 10:53:38
Go to Top of Page

savvy95
Starting Member

23 Posts

Posted - 01/03/2007 :  11:34:01  Show Profile  Reply with Quote
Jeff -- I knew you could help me.....Thank you. It works like a charm

harsh_athalye - I meant an HTML table
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000