您的位置:首页 > 博客中心 > 数据库 >

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

时间:2022-03-14 00:36

CREATE TRIGGER GetCount
on teachers
after insert
as
begin
    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

 

热门排行

今日推荐

热门手游