Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SELECT COUNT(*) FROM tblRequest GROUP BY RequestStatusID

This Query return the Column having total requests respective status Id.

I want to get total requests respective to its statusId in a output variable using store procedure.
i.e. it return a column like
______________
|NoNameColumn|
|-----------|
21
|-----------|
6
|-----------|
14
|------ ----|


I Want to get each row value in a output variable separately(eg. SET @pending=21, Set @Sent=6, SET Discarded=14 ), using store procedure and vb.net code.

In simple words.. How can i get that row's values in a variable by the sql store procedur?

How can I Do It..?

What I have tried:

I have a vb.net form having labels Like Pending Email, Delivered email, failed email etc. which show the status of emails in a request tblRequest.
I am going to make store procedure which get all counters and return it into output variable. which can I assign the the return values from store procedure to lables at the vb.net form.
Posted
Updated 24-Apr-16 23:41pm
Comments
Prasad Khandekar 25-Apr-16 2:04am    
Hello,

Create a stored procedure and loop through the cursor and assign values to the output variable. I do hope that you have a finite set of RequestStatusID and hence you know the number of output parameters to be passed.

1 solution

To assign each result to a different variable do it one by one like this:
SQL
SELECT @pending=COUNT(*) FROM tblRequest WHERE RequestStatusID='pending'
SELECT @sent=COUNT(*) FROM tblRequest WHERE RequestStatusID='sent'

Alternatively you can store the results in a table variable a query that:
SQL
DECLARE @tmp TABLE(RequestStatusID ???, [Count] INT)

INSERT INTO @tmp
SELECT RequestStatusID, COUNT(*) FROM tblRequest GROUP BY RequestStatusID

SELECT @pending=[Count] FROM @tmp WHERE RequestStatusID='pending'
SELECT @sent=[Count] FROM @tmp WHERE RequestStatusID='sent'

A third option would be to parse the dataset in your application layer which IMO is the best solution.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900