sql 之表变量 循环遍历

CREATE PROC [dbo].[GetAttendanceInfo](@year int,@month int)--考勤
AS
BEGIN
DECLARE @temp TABLE
 (
     Id int,
     [FeedbackTime] datetime
 )
  DECLARE @temp1 TABLE
 (
     Description nvarchar(50),
     DeptName nvarchar(50),
     Id int,
     UserName nvarchar(50),
     AttendanceDate datetime
 )
 -- 将源表中的数据插入到表变量中
 INSERT INTO @temp([Id],[FeedbackTime])
 SELECT t2.Id,[FeedbackTime] FROM [dbo].[ISO_TravelFeedback_Main] t1 
                                            RIGHT JOIN ISO_TravelFeedback t2 ON t1.Id = t2.TFId
                                            INNER JOIN DLFlow.DBO.WF_FlowInstance_Main t3 ON t1.Id=t3.FormPK1
                                            WHERE Datename(YEAR,FeedbackTime)=@year AND Datename(MONTH,[FeedbackTime])=@month AND t3.FlowId=293 AND InsStateId=3
 
 -- 声明变量
 DECLARE
     @Description as nvarchar(50),
     @DeptName as nvarchar(50),
     @Id as int,
     @UserName as nvarchar(50),
     @AttendanceDate as datetime
     
 WHILE EXISTS(SELECT Id FROM @temp)
 BEGIN
     -- 也可以使用top 1
     SET ROWCOUNT 1
     SELECT @Id=Id FROM @temp;
     INSERT INTO @temp1([Description],[DeptName],[Id],[UserName],[AttendanceDate])
     SELECT [Description],[DeptName],[Id],[UserName],[AttendanceDate] 
                                              FROM [dbo].[V_ISO_AttendanceDay]
                                              WHERE Datename(YEAR,AttendanceDate)=@year
                                              AND Datename(MONTH,AttendanceDate)=@month
                                              AND CONVERT(varchar(100), AttendanceDate, 23) IN
                                              (SELECT CONVERT(varchar(100), splitdate, 23) from dbo.DG_SplitDate(
                                               (SELECT t2.StartTime FROM [dbo].[ISO_TravelFeedback_Main] t1 
                                                RIGHT JOIN ISO_TravelFeedback t2 ON t1.Id = t2.TFId
                                                INNER JOIN DLFlow.DBO.WF_FlowInstance_Main t3 ON t1.Id=t3.FormPK1
                                                WHERE  t3.FlowId=293 AND InsStateId=3 AND t2.Id=@Id),
                                               (SELECT t2.EndTime FROM [dbo].[ISO_TravelFeedback_Main] t1 
                                                RIGHT JOIN ISO_TravelFeedback t2 ON t1.Id = t2.TFId
                                                INNER JOIN DLFlow.DBO.WF_FlowInstance_Main t3 ON t1.Id=t3.FormPK1
                                                WHERE  t3.FlowId=293 AND InsStateId=3 AND t2.Id=@Id)
                                                ) t1
                                                WHERE NOT EXISTS (SELECT CONVERT(varchar(100), HDate, 23)
                                                FROM DLPM.dbo.WH_Holidays t2 
                                                WHERE CONVERT(varchar(100), t2.HDate, 23)=CONVERT(varchar(100), t1.splitdate, 23)))
                                                AND UserName=(SELECT Traveler FROM ISO_TravelFeedback WHERE Id=@Id)
                                              
         SET ROWCOUNT 0
    DELETE FROM @temp WHERE Id=@Id;
 END
 SELECT * FROM @temp1
 DELETE FROM @temp1
END

 

转载于:https://www.cnblogs.com/LessIsMoreZ/p/9341613.html

你可能感兴趣的:(sql 之表变量 循环遍历)