0

我在数据库中有 3 个表,1 个是瑜伽课,另一个是成员以及它们之间的交集,我想创建一个存储过程,该过程将打印指定类的详细信息以及分配给班级。报告输出的格式应与以下内容相同:

YOGA CLASS REPORT:
___________________

Class ID: 1112
Week day:   Monday    [Time: 07:00:00]
Studio Number: 1

No.  Member Name         Contact Number
________________________________________
1    John Doe            +26 83 562 3953

到目前为止,我有:

CREATE PROCEDURE sp_Report  
@classID SMALLINT  
AS  
  IF NOT EXISTS(SELECT * FROM class WHERE classID = @classID)      
  BEGIN  
     RAISERROR ('classID does not exist',16,1)  
     RETURN  
  END

  DECLARE @classWeekday VARCHAR(15)  
  DECLARE @classTime TIME  
  DECLARE @classStudioNo TINYINT

  SELECT @classWeekday = class.classWeekday, @classTime = class.classTime, 
         @classStudioNo = class.classStudioNo  
  FROM class  
  WHERE @classID = classID

  PRINT 'YOGA CLASS REPORT'  
  PRINT 'Class code:' +CAST(@classID AS VARCHAR)  
  PRINT 'Week day:' +@classWeekday +'[Time:' + CAST(@classTime AS VARCHAR)+ ']'   
  PRINT 'Studio Number' +CAST(@classStudioNo AS VARCHAR)  

我现在如何打印参加特定课程的所有成员?任何帮助将不胜感激。

4

3 回答 3

0

您是否尝试过使用光标?

PRINT 'YOGA CLASS REPORT' 

DECLARE cClasses CURSOR FOR
SELECT classWeekday, classTime, classStudioNo  
FROM class  
WHERE classID = @classID

OPEN cClasses
FETCH NEXT FROM cClasses INTO @classWeekday, @classTime, @classStudioNo
WHILE @@FETCH_STATUS = 0
BEGIN

    PRINT 'Class code:' +CAST(@classID AS VARCHAR)  
    PRINT 'Week day:' +@classWeekday +'[Time:' + CAST(@classTime AS VARCHAR)+ ']'   
    PRINT 'Studio Number' +CAST(@classStudioNo AS VARCHAR)

FETCH NEXT FROM cClasses INTO @classWeekday, @classTime, @classStudioNo
END
CLOSE cClasses
DEALLOCATE cClasses

这里有 msdn DECLARE CURSOR SQL SERVER中的官方文档

于 2012-07-19T16:30:40.880 回答
0

这样的事情会给你参加每个班级的人......

SELECT 
     c.MemeberID
     , m.MemberName
     , m.ContactNumber
FROM 
     class c
     INNER JOIN members m ON
         c.MemberID = m.MemberID 
WHERE c.classID = @classID

查看这个Stack Overflow 问题,我想知道您为什么实际使用PRINT

于 2012-07-19T17:57:34.680 回答
0

正如其他人所提到的,我很好奇您为什么要为此使用 print ,但鉴于这是一个约束,您必须通过连接进行光标移动。下面是一些示例代码和一个工作示例。

编辑:我只想补充一点,通常在使用 SQL 时,您会希望避免使用游标。SQL 是一种基于集合的语言,游标是过程性的。因此,虽然我给出了一个使用游标的示例以满足必须打印每个结果的约束,但我不会在任何生产环境中这样做。

declare @class table (classID int, className nvarchar(100))
insert @class values (1,'TSQL'),(2,'SQL Server')
declare @members table (memberID int, memberName nvarchar(100), contactNumber nvarchar(100))
insert @members values (1, 'John Doe', '+26 83 562 3953'), (2, 'Bert Evans','+26 83 562 3954')
declare @classmembers table (classID int, memberID int)
insert @classmembers values (1,1),(1,2)

declare attendees cursor for 
    select rsMembers.memberID, memberName, contactNumber
    from @members rsMembers
    inner join @classmembers rsClassMembers on rsClassMembers.memberID = rsMembers.memberID
    where rsClassMembers.classID = 1
    
open attendees
declare @number int, @name nvarchar(100), @contact nvarchar(100)
fetch next from attendees into @number, @name, @contact
while @@FETCH_STATUS = 0
begin

    print cast(@number as nvarchar(100)) + ' ' + @name + ' ' + @contact
    fetch next from attendees into @number, @name, @contact
    
end
close attendees
deallocate attendees

工作示例。

于 2012-07-19T18:11:37.677 回答