如何使用2个不同的表创建存储过程?

时间:2012-07-19 16:18:56

标签: sql sql-server sql-server-2008

我在数据库中有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)  

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

3 个答案:

答案 0 :(得分: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

中的官方文档

答案 1 :(得分: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 question我想知道您实际使用PRINT的原因?

答案 2 :(得分: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

Working example.