Solved

Assign fields to other names

Posted on 2011-09-21
9
187 Views
Last Modified: 2012-05-12
Hello Experts,

I have 9 separate stored procedures that I would like to combine to just one stored procedure. Each one of the 9 stored procedures perfoms a count function. I would like to add a name to each of the 9 count functions to use later when displaying which count function is for which.

So you will see that the 9 stored procedures below all have "total" which is used to assign the count value to. I need another name to use to describe or list the name of the count funtion.

Is this possible and if so how?


SELECT COUNT(*) AS Total
FROM WellnessChoice_AnnualPhysical

SELECT COUNT(*) AS Total
FROM WellnessChoice_GeneralHealthProgramOne

SELECT COUNT(*) AS Total
FROM WellnessChoice_GeneralHealthProgramThree

SELECT COUNT(*) AS Total
FROM WellnessChoice_GeneralHealthProgramTwo

SELECT COUNT(*) AS Total
FROM WellnessChoice_HealthRiskAssessment

SELECT COUNT(*) AS Total
FROM WellnessChoice_PersonalInfo

SELECT COUNT(*) AS Total
FROM WellnessChoice_PhysicalActivity

SELECT COUNT(*) AS Total
FROM WellnessChoice_Pledge

SELECT COUNT(*) AS Total
FROM WellnessChoice_PreventiveScreenings

Open in new window

0
Comment
Question by:asp_net2
9 Comments
 
LVL 73

Expert Comment

by:sdstuber
ID: 36574081
maybe I'm misunderstanding what you're asking for,
do you just want to alias them as something different?

for example....

select count(*) as ScreeningTotal
FROM WellnessChoice_PreventiveScreenings
0
 
LVL 4

Author Comment

by:asp_net2
ID: 36574101
Hi sdstuber,

Yes, but I need to add another alias name so that I can use that in my code for the count report..
0
 
LVL 4

Expert Comment

by:jmdl1983
ID: 36574171
select count(*) from table1
union
select count(*) from table2

then each can be called as rstSearch(0), rstSearch(1), etc... where rstSearch is the result of the recordset search
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 4

Author Comment

by:asp_net2
ID: 36574190
Hi jmdl1983,

Ok, but I also need another name for each count. Please see an example as to what I need below.

Table1:
Name of Report 1      Total=8

Table2:
Name of Report 2      Total=22

etc....
0
 
LVL 4

Expert Comment

by:jmdl1983
ID: 36574270
When you do a union, the results are stacked in a single column therefore you cannot have each with a seperate name, however you can reference using their absolute position in the search result.  Example to follow:

SELECT COUNT(*) AS Total FROM WellnessChoice_AnnualPhysical
UNION SELECT COUNT(*) FROM WellnessChoice_GeneralHealthProgramOne
UNION SELECT COUNT(*) FROM WellnessChoice_GeneralHealthProgramThree
UNION SELECT COUNT(*) FROM WellnessChoice_GeneralHealthProgramTwo
UNION SELECT COUNT(*) FROM WellnessChoice_HealthRiskAssessment
UNION SELECT COUNT(*) FROM WellnessChoice_PersonalInfo
UNION SELECT COUNT(*) FROM WellnessChoice_PhysicalActivity
UNION SELECT COUNT(*) FROM WellnessChoice_Pledge
UNION SELECT COUNT(*) FROM WellnessChoice_PreventiveScreenings

Then they may be referenced as:

WellnessChoice_AnnualPhysical = rstSearch(0)
WellnessChoice_GeneralHealthProgramOne = rstSearch(1)
WellnessChoice_GeneralHealthProgramThree = rstSearch(2)
WellnessChoice_GeneralHealthProgramTwo = rstSearch(3)
WellnessChoice_HealthRiskAssessment = rstSearch(4)
WellnessChoice_PersonalInfo = rstSearch(5)
WellnessChoice_PhysicalActivity = rstSearch(6)
WellnessChoice_Pledge = rstSearch(7)
WellnessChoice_PreventiveScreenings = rstSearch(8)
0
 
LVL 4

Author Comment

by:asp_net2
ID: 36574331
@jmdl1983,

Ok, that's not going to work for what I need then. What about adding another name to the following SP below then?

I understand that the Count will be assigned to "total" but is there a way that I can add the PK field and then assign a unique name to the PK?

SELECT COUNT(*) AS Total
FROM WellnessChoice_AnnualPhysical

SOMETHING LIKE THE FOLLOWING BELOW.

SELECT COUNT(*) AS Total, ap_id = Annual Physical Report
FROM WellnessChoice_AnnualPhysical
0
 
LVL 32

Accepted Solution

by:
ewangoya earned 500 total points
ID: 36574370
you can do this
SELECT 
	(SELECT COUNT(*) FROM WellnessChoice_AnnualPhysical) AnnualPhysicalTotal,
	(SELECT COUNT(*) FROM WellnessChoice_GeneralHealthProgramOne) GeneralHealthProgramOneTotal,
	(SELECT COUNT(*) FROM WellnessChoice_GeneralHealthProgramThree) GeneralHealthProgramThreeTotal,
	(SELECT COUNT(*) FROM WellnessChoice_GeneralHealthProgramTwo) GeneralHealthProgramTwoTotal,
	(SELECT COUNT(*) FROM WellnessChoice_HealthRiskAssessment) HealthRiskAssessmentTotal,
	(SELECT COUNT(*) FROM WellnessChoice_PersonalInfo) PersonalInfoTotal,
	(SELECT COUNT(*) FROM WellnessChoice_PhysicalActivity) PhysicalActivityTotal,
	(SELECT COUNT(*) FROM WellnessChoice_Pledge) PledgeTotal,
	(SELECT COUNT(*) FROM WellnessChoice_PreventiveScreenings)PreventiveScreeningsTotal

Open in new window

0
 
LVL 4

Author Comment

by:asp_net2
ID: 36574462
@ewangoya,

Ok, that looks good. BUT, I still need a way to assign a value to each of those so that I know which count is for which table. In other words I need two values. One for the value that will hold the count data and another value to represent the data.
0
 
LVL 4

Expert Comment

by:jmdl1983
ID: 36574602
You can use @ewangoya's solution, and then reference each value by the name ie....

rstSearch("WellnessChoice_AnnualPhysical")
rstSearch("WellnessChoice_GeneralHealthProgramOne")
rstSearch("WellnessChoice_GeneralHealthProgramThree")
rstSearch("WellnessChoice_GeneralHealthProgramTwo")
rstSearch("WellnessChoice_HealthRiskAssessment")
rstSearch("WellnessChoice_PersonalInfo")
rstSearch("WellnessChoice_PhysicalActivity")
rstSearch("WellnessChoice_Pledge")
rstSearch("WellnessChoice_PreventiveScreenings")
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question