473,769 Members | 3,893 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.CreateOb ject("ADODB.Rec ordset")
Sql = "SELECT ID from Verified"
rsCount.Open SQL, Conn, 1, 3

totalCount = rsCount.recordc ount
rsCount.close
set rsCount = nothing

Set rsACount = Server.CreateOb ject("ADODB.Rec ordset")
Sql = "SELECT PetA from Verified WHERE PetA ='1'"
rsACount.Open SQL, Conn, 1, 3

PetACount = rsACount.record count
rsACount.close
set rsACount = nothing

Set rsBCount = Server.CreateOb ject("ADODB.Rec ordset")
Sql = "SELECT PetB from Verified WHERE PetB ='1'"
rsBCount.Open SQL, Conn, 1, 3

PetBCount = rsBCount.record count
rsBCount.close
set rsBCount = nothing

Set rsCCount = Server.CreateOb ject("ADODB.Rec ordset")
Sql = "SELECT PetC from Verified WHERE PetC ='1'"
rsCCount.Open SQL, Conn, 1, 3

PetCCount = rsCCount.record count
rsCCount.close
set rsCCount = nothing

Set rsDCount = Server.CreateOb ject("ADODB.Rec ordset")
Sql = "SELECT PetD from Verified WHERE PetD ='1'"
rsDCount.Open SQL, Conn, 1, 3

PetDCount = rsDCount.record count
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 1503
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.CreateOb ject("ADODB.Rec ordset")
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,PetA Count,PetBCount ,PetCCount,PetD Count
'create and open the connection, then:
Set rs = Server.CreateOb ject("ADODB.Rec ordset")
conn.GetPetCoun ts rs
if not rs.eof then ardata=rs.GetRo ws
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=curCo unt
case "B": PetBCount=curCo unt
case "C": PetCCount=curCo unt
case "D": PetDCount=curCo unt
case "Total": PetACount=total Count
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
3105
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 domains. Then I saw this as Stroustrup's FAQ of the day: http://www.research.att.com/~bs/bs_faq.html#C-is-better Q:"C is better than C++ for small projects, right?" A:"Not in my opinion. I never saw a project for which C was better than C++
3
2423
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 variety of > shows are presented there, for example plays, music > and talks. The management has decided to build a > computer system for WCT to handle ticket sales, keep > track of the work done by staff and record all shows > presented in the...
37
2785
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
2387
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 follow for creating a program? I am writing a map game program. I created several objects: board object that is an array of integers each number 0-5 is a kind of terrain, a terrain object that is an array of terrain types and each number of...
6
1106
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 2002 Ford Mustang 2004 Ford Mustang 2005
17
5414
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 think it is mostly not going to happen for ++ operation since it is not something like read a value and wait sometime then write back in multiple threading environment, BTW, is this understanding right?).
1
1288
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). I would like to know where i will going to fit as an entry level in unix.I completed my P.G(MCA)in 2005.I know all the unix basic commands,How to conncet to database...etc .Pls give me some guidence....
0
764
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
5080
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 talking about the non-OO stuff, that seems to be handled much more elegantly in C++, as compared to C. For example new & delete, references, consts, declaring variables just before use etc. I am asking this question with a vested interest. I...
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10211
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10045
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7408
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3958
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.