我有以下选择:
SELECT School_Type,COUNT(ID) from Schools where City_ID = 1 group by School_Type
我得到了结果:
10 | 3
20 | 4
30 | 14
我想把结果放在:
@ElementarySchools
@HighSchools
@ProfessionalSchools
并从存储过程中获取此结果。
我该怎么做?
答案 0 :(得分:0)
declare @val varchar(max) = ''
select @val = @val + rtrim(foryear) + ' | ' + RTRIM( COUNT(*)) + ',' from mytable
group by ForYear
select @val
答案 1 :(得分:0)
使用像这样的表变量:
declare @tmp table (School_Type int, School_Count int)
insert into @tmp
SELECT School_Type,COUNT(ID) from Schools where City_ID = 1 group by School_Type
select @ElementarySchools=School_Count from @tmp where School_Type=10
select @HighSchools=School_Count from @tmp where School_Type=20
select @ProfessionalSchools=School_Count from @tmp where School_Type=30