while循环控制表的样式

来源:互联网 发布:暴雪伊利丹手办淘宝 编辑:程序博客网 时间:2024/05/18 02:35

select AwardID,t.TypeID,
DepartmentName,UserName,
UG_Name,T.AwardType,
T.AwardReason,convert(nvarchar(10),T.AwardTime,121) as 'AwardTime',T.Inactive
into #temp
from T_GLGZ_AwardInfo T
left join  View_UserInfo v on t.userid= v.userid
where inactive = 0

declare @PrimaryIDTable table
(
 AwardID int,
 typeid nvarchar(10),
 DepartmentName nvarchar(50),
 UserName nvarchar(50),
 UG_Name nvarchar(50),
 AwardType nvarchar(50),
 AwardReason nvarchar(100),
 AwardTime nvarchar(50),
 Inactive nvarchar(10)
)

--记录奖励类型
declare @typecount int
set @typecount = 0
while @typecount <=1
begin
 --统计总条数
 declare @count  int
 --记录总条数
 declare @recount  int
 set @count = (select count(1) from T_GLGZ_AwardInfo where TypeID =@typecount)
 set @recount = @count
  WHILE @count >=0
    BEGIN
     if @recount = @count
      begin
       insert into @PrimaryIDTable
       select top 1* from #temp where typeid = @typecount
      end
     else if @recount != 0 and  @count = 0
      begin
       insert into @PrimaryIDTable
       select '','合计',count(1),'','','','','','' from #temp where typeid = @typecount
      end
     else
      begin
       insert into @PrimaryIDTable
       select AwardID,'',DepartmentName,UserName,UG_Name,AwardType,AwardReason,AwardTime,Inactive from #temp
       where AwardID not in(select AwardID from @PrimaryIDTable) and typeid = @typecount
      end
      SET @count = @count - 1 
    END
   SET @typecount = @typecount + 1
end
select AwardID,case typeid when '0' then '奖励' when  '1' then '惩罚' when '合计' then '合计' end as 'TypeID',
DepartmentName,UserName,UG_Name,AwardType,AwardReason,AwardTime,Inactive
from @PrimaryIDTable

drop table #temp

 

原创粉丝点击