473,387 Members | 1,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

I am sure there is a better way, looking for some guidence!

Query in ASP against a MS SQL DB.

<%
Dim Sql
Dim Conn
Dim totalCount
Dim rsCount
Dim rsACount
Dim PetACount
Dim rsBCount
Dim PetBCount
Dim rsCCount
Dim PetCCount
Dim rsDCount
Dim PetDCount

Set rsCount = Server.CreateObject("ADODB.Recordset")
Sql = "SELECT ID from Verified"
rsCount.Open SQL, Conn, 1, 3

totalCount = rsCount.recordcount
rsCount.close
set rsCount = nothing

Set rsACount = Server.CreateObject("ADODB.Recordset")
Sql = "SELECT PetA from Verified WHERE PetA ='1'"
rsACount.Open SQL, Conn, 1, 3

PetACount = rsACount.recordcount
rsACount.close
set rsACount = nothing

Set rsBCount = Server.CreateObject("ADODB.Recordset")
Sql = "SELECT PetB from Verified WHERE PetB ='1'"
rsBCount.Open SQL, Conn, 1, 3

PetBCount = rsBCount.recordcount
rsBCount.close
set rsBCount = nothing

Set rsCCount = Server.CreateObject("ADODB.Recordset")
Sql = "SELECT PetC from Verified WHERE PetC ='1'"
rsCCount.Open SQL, Conn, 1, 3

PetCCount = rsCCount.recordcount
rsCCount.close
set rsCCount = nothing

Set rsDCount = Server.CreateObject("ADODB.Recordset")
Sql = "SELECT PetD from Verified WHERE PetD ='1'"
rsDCount.Open SQL, Conn, 1, 3

PetDCount = rsDCount.recordcount
rsDCount.close
set rsDCount = nothing

conn.close
set conn = nothing
%>

Help please to clean and speed it up! Looking for 5 record counts, Total,
PetA, PetB, PetC, and PetD.

Thanks
Jeff
Jul 19 '05 #1
1 1475
Jeff Uchtman wrote:
Query in ASP against a MS SQL DB.

<%
Dim Sql
Dim Conn
Dim totalCount
Dim rsCount
Dim rsACount
Dim PetACount
Dim rsBCount
Dim PetBCount
Dim rsCCount
Dim PetCCount
Dim rsDCount
Dim PetDCount

Set rsCount = Server.CreateObject("ADODB.Recordset")
Sql = "SELECT ID from Verified"
rsCount.Open SQL, Conn, 1, 3

<snip>

Yes there is definitely a better way than making 5 trips to the database to
open very expensive cursors:
1. Normalize your database. You are having a hard time here because you have
4 PetX columns. Instead, you should break out the Pet information into
another table called Pets. The table will have 3 columns: ID, PetType,
PetData (I'm not sure what the 1's represent. You will probably want to
replace "PetData" with a more descriptive name). This will cause you to
rearrange this data:

ID PetA PetB PetC PetD
A 1 1
B 1 1 1

to look like this:

Verified
ID
A
B
Pets
ID PetType PetData
A A 1
A C 1
B B 1
B C 1
B D 1

If for some reason, you don't have control over this database design, then,
instead of creating a new table, you should create a view that normalizes
the table. This view will "fold" your table, normalizing it:

CREATE VIEW Pets AS
SELECT ID, 'A' PetType,PetA PetData
FROM Verified
WHERE PetA is not null
Union All
SELECT ID, 'B', PetB FROM Verified
WHERE PetB is not null
Union All
SELECT ID, 'C', PetC FROM Verified
WHERE PetC is not null
Union All
SELECT ID, 'D', PetD FROM Verified
WHERE PetD is not null

Either of these options (the new table, or the new view) will allow you to
get the total count from Verified by:

SELECT Count(*) as [Total] FROM Verified

and the pet counts in a single query by:
SELECT PetType, Count(*) As PetTotal
FROM Pets
WHERE PetData = '1'
GROUP BY PetType
2. Use a stored procedure. Use Query Analyzer to run this script in your
database to create the stored procedure.

CREATE PROCEDURE GetPetCounts
AS

SELECT PetType, Count(*) As PetTotal
FROM Pets
WHERE PetData = '1'
GROUP BY PetType
UNION ALL
SELECT 'Total', Count(*) FROM Verified

Then test the stored procedure, again using Query Analyzer, to make sure it
returns the results you want. Based on the above data, this procedure should
return the following resultset:

PetType Total
A 1
B 1
C 2
D 1
Total 2
In ASP, you can call this stored procedure very efficiently by this vbscript
code:

dim ardata, rs, conn, i, curCount
Dim totalCount,PetACount,PetBCount,PetCCount,PetDCount
'create and open the connection, then:
Set rs = Server.CreateObject("ADODB.Recordset")
conn.GetPetCounts rs
if not rs.eof then ardata=rs.GetRows
rs.close: set rs=nothing
conn.close: set conn=nothing
if isarray(ardata) then
for i = 0 to ubound(ardata,2)
curCount = ardata(1,i)
select case ardata(0,i)
case "A": PetACount=curCount
case "B": PetBCount=curCount
case "C": PetCCount=curCount
case "D": PetDCount=curCount
case "Total": PetACount=totalCount
end select
next
else
response.write "no counts were returned"
end

HTH,
Bob Barrows
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Steven T. Hatton | last post by:
You can scroll down to the last line of this post in order to find the question I really want to discuss. I was just pondering the competing applicability of C vs. C++ in certain problem...
3
by: solomon_13000 | last post by:
> Wonthaggi Civic Theatre 'WCT' Case Study > > The town of Wonthaggi has a theatre which is owned and > operated by the local council, it is called the > Wonthaggi Civic Theatre (WCT) and a wide...
37
by: yogpjosh | last post by:
Hello All, I was asked a question in an interview.. Its related to dynamically allocated and deallocated memory. eg. //start char * p = new char; ...
23
by: JoeC | last post by:
I am a self taught programmer and I have figured out most syntax but desigining my programs is a challenge. I realize that there are many ways to design a program but what are some good rules to...
6
by: hartbypass | last post by:
Hello, I'll try to explain this the best I can. I am using VS2005/c# and this is a web app. I have some data in the format of: Ford Focus 2000 Ford Focus 2001 Ford Focus ...
17
by: Ryan Liu | last post by:
Hi, If I have many threads write to a variable(e.g. var++) and another thread read it on an interval base. For those writing thread, I know I need lock, or its value could be lower ( even I...
1
by: sandhya rani | last post by:
Hai all the forum members, Sorry for putting such questions to u all but really i don't know abt how to get jobs in unix(shell scripting & database). ...
0
by: mumbaicnu | last post by:
hi,i'm srini, i need some documentation which cneed to cover webservices concept in .net. or provide some link which provide information for this....
204
by: Masood | last post by:
I know that this topic may inflame the "C language Taleban", but is there any prospect of some of the neat features of C++ getting incorporated in C? No I am not talking out the OO stuff. I am...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.