Important alert: (current site time 7/15/2013 10:33:39 PM EDT)
 

VB icon

A ADO Data Shaping or Multiple SQL Select

Email
Submitted on: 2/27/2001 3:58:01 PM
By: Rob Gerwing  
Level: Intermediate
User Rating: By 6 Users
Compatibility: ASP (Active Server Pages)
Views: 64390
(About the author)
 
     Do you have slow running mulitple SELECT Statements or long reports to fill on a web page. Use the Microsoft Shape Command. Learn to use ADO 2.1 and greater advance features. This code is great for three things, (1) Very fast way to do multiple SQL select statements and reports. (2) Great for databases not Normalized. (3) Avoids multiple nested single threaded ADO Record Sets loops which are very slow.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: A ADO Data Shaping or Multiple SQL Select
' Description:Do you have slow running mulitple SELECT Statements or long reports to fill on a web page. Use the Microsoft Shape Command. Learn to use ADO 2.1 and greater advance features. This code is great for three things, (1) Very fast way to do multiple SQL select statements and reports. (2) Great for databases not Normalized. (3) Avoids multiple nested single threaded ADO Record Sets loops which are very slow.
' By: Rob Gerwing
'
' Assumes:Basic ADO Recordset use in Microsoft Active server Pages. Basic Knowledge of SQL "SELECT" statements.
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=6512&lngWId=4'for details.'**************************************

<%Response.Buffer = true
Const adOpenForwardOnly = 2
Dim connShape,strShape
Dim objConn,objRS,objStartDate,objEndDate
'ADO CONNECTION
Set connShape = Server.CreateObject("ADODB.Connection")
connShape.Provider = "MSDataShape" 'Tell ADO to expect MSShape Command in SQL Syntax
connShape.Open "DSN=NAME-OF-ODBC-SOURCE" 'Insert your Data Source Name String
'ADO RECORDSET
Set objRS = Server.CreateObject("ADODB.Recordset")
'Shape SQL Syntax
[Available at Microsoft KB Article Q189657]
'WHY DID I USE SHAPE AND NOT A JOIN?
'Look at the 2nd and 3rd SELECTS, I needed to retrieve a record associated to
'order_id BUT the same field name "event_value" and different event_types.
'Working Example -->
	strShape = "SHAPE {Select order_id,f_name,l_name FROM Order_Table"&_
	" WHERE l_name = 'SMITH' ORDER BY l_name} AS OrderData "&_
		 "APPEND "&_
		 "({ SELECT order_id, event_value, event_type FROM event" &_
		 " WHERE event_type = 'UserStartDate' } " &_
		 " RELATE order_id TO order_id) AS STARTDATE, "&_
		 " ({ SELECT order_id, event_value, event_type FROM event" &_
		 " WHERE event_type = 'UserEndDate' } " &_
		 " RELATE order_id TO order_id) AS ENDDATE"
'Open RecordSet
objRS.OPEN strShape,ConnShape,adOpenForwardOnly
Response.Write "<TABLE BORDER=1 CELLPADDING=0 CELLSPACING=0>"
Do While Not objRS.EOF 'Looping through Parent Record Set
'Take from Parent Record Set
Response.Write("<TR><TD>" & objRS("order_id") & "</TD>")
Response.Write("<TD>" & objRS("l_name") & ", " & objRS("f_name") & "</TD>")
	
'StartDate 1st Child RecordSet No Loop, EXPECTING ONLY ONE RECORD VALUE
	'Must use "STARTDATE" as reference in SQL "AS STARTDATE"
	Set objStartDate = objRS("STARTDATE").Value 
		 
	If objStartDate.Eof = True Then
		Response.Write("<TD> </TD>")
	Else
		Response.Write("<TD>" & objStartDate("event_value") & "</TD>")
	End If
			
	objStartDate.Close
				
'EndDate 2nd Child RecordSet Loop Used,EXPECTING MORE MULTIPLE RECORD VALUES
	'Must use "ENDDATE" as reference in SQL "AS ENDDATE"
	Set objEndDate = objRS("ENDDATE").Value
		
	If objEndDate.Eof = True Then
		Response.Write("<TD> </TD>")
	Else
		Response.Write("<TD>")
			while not objEndDate.Eof
				 Response.write (objEndDate("event_value") & ",")
			objEndDate.MoveNext
			wend
		Response.Write("</TD>")
	End If
			
		objEndDate.Close
Response.Write "</TR>"
objRS.MoveNext 
LOOP
Response.Write "</TABLE>"
'Clean Up
connShape.Close
objRS.Close
Set connShape = Nothing
Set objRS = Nothing
%>


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

2/28/2001 5:43:56 PMJerry MSDN User

I been looking how to do this, This loads the Recordset so fast. My old script had to find the indentifier, then loop through. My clients are very happy campers now!
(If this comment was disrespectful, please report it.)

 
2/28/2001 8:11:26 PMJohn W.

Thanks ... it helped.....
(If this comment was disrespectful, please report it.)

 
3/11/2001 6:29:56 PMEugene

Thanks. This is very Helpful.
(If this comment was disrespectful, please report it.)

 
5/13/2002 1:35:38 AMjimmy_thakkar

very veryy complexxxx
(If this comment was disrespectful, please report it.)

 
8/24/2002 1:21:33 AMFendi

It's definitely meaningful. I able to undertand the
(If this comment was disrespectful, please report it.)

 
3/14/2003 1:37:56 PM

Brillant!! Theres Any Way I can Use a StoreProcedure using this sintaxis?
(If this comment was disrespectful, please report it.)

 
3/14/2003 2:16:22 PMRob Gerwing

I have not found documentation on using ADO Shape command in a store procedure. If I remember, MSSQL will not let you compile a Stored Proc with this syntax. So even though Command object will look at Store Proc as text syntax, the actual creation of the store proc on the SQL server is the limitation. I haven't tried in SQL 2000. Anybody let me know if it works?
(If this comment was disrespectful, please report it.)

 
1/15/2004 1:55:48 AM

hai sir,
i am amin i commited to my friend to do a simple website but i don't know how to access the access database in html or asp you please guide me my mail id is aminind2003@yahoo.com
(If this comment was disrespectful, please report it.)

 
1/15/2004 1:57:24 AM

aminind2003@yahoo.co.in

i am commited to my friend that i will do a simple project but i don't know how to acess the access in the html or asp please guide me !!!
(If this comment was disrespectful, please report it.)

 
1/15/2004 1:59:47 AM

hai sir i am amin aminind2003@yahoo.co.in i am commited to my friend to make a simple website i don't know how to add the access in html or asp..

guide me !!!
(If this comment was disrespectful, please report it.)

 
11/25/2005 1:10:24 AM

hi sir..
i'm doing a project and it requires me to get datas from database using FrontPage. is there any much simple method ?
(If this comment was disrespectful, please report it.)

 
1/3/2010 2:52:00 AMnagendra

i was nice to help
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.