SQL Server 存储过程 循环遍历结果集

来源:互联网 发布:企业办公软件排名 编辑:程序博客网 时间:2024/05/03 18:16
USE [Task]GO/****** Object:  StoredProcedure [dbo].[pro_name]    Script Date: 09/22/2013 10:35:09 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOALTER procedure [dbo].[pro_name]    @tid int,    @ap floatasbeginSET NOCOUNT ON;DECLARE @pid intDECLARE @AlreadyPercent floatDECLARE @Percents floatDECLARE @total floatif(@ap > 100)beginset @ap = 100;end--更新记录update tbTask set AlreadyPercent = round(convert(numeric(6,3),@ap),0) where ID = @tid;print round(convert(numeric(6,3),@ap),0);SET @pid = (select top 1 tbTask.ParentTaskID from tbTask where tbTask.ID=@tid);--如果记录存在,进入循环    IF(@pid is not null and @pid != 0)    BEGINset @total = 0;    DECLARE vend_cursor CURSORFOR SELECT tbTask.AlreadyPercent,tbTask.Percents FROM tbTask where tbTask.ParentTaskID = @pidOPEN vend_cursorFETCH NEXT FROM vend_cursor into @AlreadyPercent,@Percents;                WHILE(@@fetch_status=0)        BEGINset @total = @total + @AlreadyPercent * @Percents / 100;            FETCH NEXT FROM vend_cursor into @AlreadyPercent,@Percents;        END        --print 'total = ' + cast(@total as varchar);        close vend_cursor   --关闭游标        deallocate vend_cursor        exec pro_name @pid, @total;    ENDend

原创粉丝点击