0
 declare @temp int,
@temp1 int,
@temp2 int,
@temp3 int,
@temp4 int,
@temp5 int,
@modid int,
@supid int,
@sid varchar(50)
begin tran
select * from StudentSupervisor;

select @temp = count(*) from Students s where s.IsLockedOut = '0' and s.IsGraduated = '0'; 
select @temp1 = count(*)  from staffs st where st.IsLockedOut ='0';



set @temp5 = round(@temp/@temp1,0);

WHILE (select count(*) from students s where s.IsLockedOut ='0' and s.StudentId not in (select ss.StudentId from StudentSupervisor ss where s.StudentId = ss.StudentId and ss.IsApproved = 1)) != 0

BEGIN


select top 1 @sid = s.studentid from students s, StudentSupervisor ss where s.IsLockedOut ='0' and s.StudentId not in (select s.StudentId where s.StudentId = ss.StudentId and ss.IsApproved = 1);


select top 1 @supid = st.Staffid, @modid = st.moderatorid from Staffs st where st.IsLockedOut =0 and Quota <=@temp5;

        insert into StudentSupervisor 
        (StudentId,SupervisorId,ModeratorId,IsApproved)
    values
        (@sid,@supid,@modid,1)
        update Staffs set quota +=1 where staffs.StaffID = @supid;

END


select * from StudentSupervisor;

ROLLBACK  tran

大家好,我非常坚持这个逻辑,我确实在寻找解决方案,但在通宵工作后我什么也没得到,现在让我弄清楚我的情况,首先我想计算不在 studentsupervisor 表或 studentsupervisor 表中的学生但是被批准!= 1,然后我计算配额不超过总学生/总教职员工的人数,之后我想在他们仍然可用的时候将学生和教职员工一起抽到学生主管表中。请让我知道什么我的动态查询有问题,谢谢

4

1 回答 1

0

我无法理解您的问题。我认为你的循环条件应该是:

WHILE (SELECT COUNT(*) from students WHERE StudentID NOT IN (SELECT StudentID FROM StudentSupervisor WHERE IsAppoved = 1)) <> 0

根据您在循环中尝试执行的操作,可能会删除循环。如果您可以使用有关循环中发生的事情的更多详细信息和示例更新您的问题,我将尝试提供更完整的答案。

更新

在这种情况下,我还将循环中的第一行更新为:

select top 1 @sid = studentid from students WHERE IsLockedOut = 0 and StudentId NOT IN (SELECT StudentID FROM StudentSupervisor WHERE IsAppoved = 1);

Update2表变量选项:

declare @temp int,
    @temp1 int,
    @temp2 int,
    @temp5 int,
    @PairCount int,                         --The number of students paired with a staff member.
    @modid int,
    @supid int,
    @StaffQuotaNeed int,                    --The number of students the staff needs to be paired with to meet quota.
    @sid varchar(50)
DECLARE @StaffToPair table
    (
        StaffID int NOT NULL,
        ModeratorID int NOT NULL,
        QuotaNeed int NOT NULL          --The number of students the staff needs to be paired with to meet quota.
    );
DECLARE @StudentsToPair table (StudentID int NOT NULL);
DECLARE @StudentsPaired table (StudentID int NOT NULL);

begin tran
select * from StudentSupervisor;

select @temp = count(*) from Students where IsLockedOut = '0' and IsGraduated = '0'; 
select @temp1 = count(*)  from staffs where IsLockedOut ='0';
set @temp5 = round(@temp/@temp1 + .5, 0);                   --Round Up

INSERT INTO @StaffToPair (StaffID, ModeratorID, QuotaNeed)
                    SELECT StaffID, ModeratorID, @temp5 - Quota FROM Staffs WHERE IsLockedOut =0 AND Quota <@temp5;
INSERT INTO @StudentsToPair (StudentID)
                    SELECT StudentID from students WHERE IsLockedOut = 0 AND StudentId NOT IN (SELECT StudentID FROM StudentSupervisor WHERE IsAppoved = 1);

WHILE (SELECT COUNT(*) from @StudentsToPair) > 0 AND (SELECT COUNT(*) from @StaffToPair) > 0
BEGIN
    SELECT TOP 1 @supid = Staffid, @modid = ModeratorID, @StaffQuotaNeed = QuotaNeed FROM @StaffToPair;

    INSERT INTO StudentSupervisor   (StudentID, SupervisorId, ModeratorId, IsApproved)
        OUTPUT INSERTED.StudentID INTO @StudentsPaired(StudentID)
        SELECT TOP (@StaffQuotaNeed) StudentID,     @supid,     @modid,         1 FROM @StudentsToPair;

    DELETE FROM @StudentsToPair WHERE StudentID IN (SELECT StudentID FROM @StudentsPaired); --Delete paired students from table variable.
    DELETE FROM @StaffToPair WHERE StaffID = @supid;                                        --Delete paired staff from table variable.

    SELECT @PairCount = COUNT(*)  FROM @StudentsPaired;
    UPDATE Staffs set Quota += @PairCount where staffs.StaffID = @supid;
END


select * from StudentSupervisor;

ROLLBACK  tran

如果上述方法不起作用,请更新您的问题以包含表架构,以便我可以在我的系统上对其进行测试。

于 2012-12-26T03:44:36.133 回答