使用 SQL 在报告中迭代

Iteration in a Report using SQL

我需要创建一个存储过程来打印有关所选 class(文字瑜伽 class,而不是编程术语)的报告。这 3 个表是 Member、Class 和 Attendance。 Attendance 的主键由其他两个的外键组成,并包含一个名为 totalPresent 的变量,可以跟踪谁在每个 class.

USE suzi_yoga_studio
GO

CREATE PROCEDURE sp_Report

@classRequiredID VARCHAR(5)
AS

SELECT class.classID, class.classTime, class.classDay, class.classStudio,
member.memID, member.memName, member.memTel, attendance.classesID, attendance.memberID
INTO #tempReport
from class
JOIN member ON member.memID = class.classID
JOIN attendance ON attendance.memberID = memberID

ALTER TABLE #tempReport
ADD
Printed SMALLINT
GO

UPDATE
SET Printed = 0
GO

DECLARE @classID INT
DECLARE @classsDay VARCHAR(15)
DECLARE @classTime VARCHAR(40)
DECLARE @classStudio VARCHAR(5)
DECLARE @memberID INT
DECLARE @memberName VARCHAR(50)
DECLARE @memberTel VARCHAR(10)

WHILE EXISTS (SELECT * FROM #tempReport WHERE Printed = 0)

BEGIN
    SELECT @classID = MIN(class.classID)
    FROM #tempReport
    WHERE Printed = 0
SELECT @classDay = #tempReport.classDay,
       @classTime = #tempReport.classTime,
       @classStudio = #tempReport.classStudio,
       @memberName = #tempReport.memName,
       @memberTel = #tempReport.memTel
FROM #tempReport

PRINT 'YOGA CLASS REPORT'
PRINT 'Week Day:   '+@classDay
PRINT 'Time:    "+@classTime
PRINT 'Studio:  '+@classStudio

此时,程序需要列出特定 class 中的成员并为其编号。我不知道该怎么做。我将不得不使用 @memberName@memberTel 但我不知道如何遍历它们。

UPDATE #tempReport
SET Printed = 1

END
GO

我该怎么做呢?它只是不适合我。

If Object_Id('tempdb.dbo.#tempReport') Is Not Null
Begin
    Drop Table #tempReport;
End

If Object_Id('tempdb.dbo.#tempReport') Is Null
Begin
    Create Table #tempReport
    (
         Id             Int Identity(1,1) Primary Key
        ,classID        Int
        ,classTime      Datetime
        ,classDay       Int
        ,classStudio    Varchar(100)
        ,memID          Int
        ,memName        Varchar(100)
        ,memTel         Varchar(100)
        ,classesID      Int
        ,memberID       Int
    )
End

Insert Into #tempReport
(
     classID
    ,classTime
    ,classDay
    ,classStudio
    ,memID
    ,memName
    ,memTel
    ,classesID
    ,memberID
)
SELECT   class.classID
        ,class.classTime
        ,class.classDay
        ,class.classStudio
        ,member.memID
        ,member.memName
        ,member.memTel
        ,attendance.classesID
        ,attendance.memberID
From    class With (Nolock)
        JOIN member With (Nolock) ON member.memID = class.classID
        JOIN attendance With (Nolock) ON attendance.memberID = memberID

DECLARE  @classID       INT
        ,@classDay      VARCHAR(15)
        ,@classTime     VARCHAR(40)
        ,@classStudio   VARCHAR(5)
        ,@memberID      INT
        ,@memberName    VARCHAR(50)
        ,@memberTel     VARCHAR(10)
        ,@totalRows     Int
        ,@curRow        Int

Select   @totalRows = Count(1)
        ,@curRow = 1
From    #tempReport As tr With (Nolock)

While (@curRow <= @totalRows)
Begin
    SELECT   @classDay = tr.classDay
            ,@classTime = tr.classTime
            ,@classStudio = tr.classStudio
            ,@memberName = tr.memName
            ,@memberTel = tr.memTel
    FROM    #tempReport As tr With (Nolock)
    Where   tr.Id = @curRow

    PRINT 'YOGA CLASS REPORT'
    PRINT 'Week Day:   ' + @classDay
    PRINT 'Time:    ' + @classTime
    PRINT 'Studio:  ' + @classStudio

    Select  @curRow = @curRow + 1
End