如何根据条件计算Exec存储过程输出值的计数?

时间:2017-08-24 09:37:42

标签: sql-server

我有一个名为&#34的存储过程名称; sp_Proc_EmployeeDetails"

在此过程中返回如下格式的值:

SNo   EmpName  EmpNumber
----------------------------
1       abc      101
2       abc      102
3       abc1     103
4       abc2     104
5       abc2     105
6       abc2     106

在上面的结果中,我需要在执行存储过程时输出以下内容。

E.g:
Exec sp_Proc_EmployeeDetails

 Output:
 -------------
 abc Count  = 2
 abc1 Count  = 1
 abc2 Count = 3

1 个答案:

答案 0 :(得分:0)

Create table Emp (
SNo  int ,
EmpName varchar(50),
EmpNumber  int
) 
go

insert into  Emp 
select 1       ,'abc'     , 101
union all select  2       ,'abc'      ,102
union all select  3       ,'abc1'     ,103
union all select  4       ,'abc2'     ,104
union all select  5       ,'abc2'     ,105
union all select  6       ,'abc2'     ,106
go

create proc sp_Proc_EmployeeDetails
as 
begin
    select EmpName + ' Count = ' + cast( COUNT(*) as varchar(10)) as  [Output:] from  Emp 
group by EmpName
end 
go

exec sp_Proc_EmployeeDetails

Output:
--------------------- 
abc Count = 2
abc1 Count = 1
abc2 Count = 3