while循环将数据插入到sql中的表中

时间:2012-12-26 03:19:02

标签: sql-server tsql while-loop

 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表中但是已经批准了!= 1,然后我计算了配额不超过学生总数/总工作人员的人数,之后我想将学生和员工一起带到学生管理员表中,但他们仍然可用..请让我知道我的动态查询有什么问题,谢谢

1 个答案:

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

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