首页 > 代码库 > SQL_自动统计表中数据的条数

SQL_自动统计表中数据的条数

CREATE TRIGGER GetCounton teachersafter insertasbegin    declare @totalCount int--表变量里面总共的变量条数    declare @RowNo int--当前是多少行    declare @tempId int--保存当前行的id    declare @classCount int--保存当前班级的数量    declare @temp1 table(indexid int identity(1,1) not null,id int)    insert into @temp1 select ID from Students     set @totalCount=@@ROWCOUNT    set @RowNo=1    while @RowNo <= @totalCount    begin        select @tempId=id from @temp1 where indexid = @RowNo        select @classCount=COUNT(*) from Students where ClassName=@tempId        update teachers set classCount=@classCount where ID=@tempId        set @RowNo = @RowNo+1    end     end

 

SQL_自动统计表中数据的条数