Solved

Assign fields to other names

Posted on 2011-09-21
9
188 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 74

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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

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.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

831 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