0

私はこのSPを持っています.UserIDとSectionIDを渡すことで学生にセクションIDを追加することを想定していますが、うまくいきます.

--split function return table contain 2 columns ID and vItem
ALTER PROC [dbo].[StudentDistribution] --'1,3,16,17,18,19','1,2,3',1,1   
(
@pUserID varchar(8000),
@pSectionID varchar(8000),
@pClassID int,
@pModifiedBy int    
)
as

DECLARE @vCntr int  
DECLARE @vSectionCounter int
DECLARE @vCapacity int 
DECLARE @vCurrentStudent int
DECLARE @vCurrentCapacity int

SET @vSectionCounter = (select count(vItem) from split(@pSectionID,','))
SET @vCntr = (select count(*) from split(@pUserID ,','))
SET  @vCapacity = (select sum(Capacity) from SectionsClasses where ClassID=@pClassID and SectionID in (select vItem from split(@pSectionID,',')))
SET @vCurrentStudent = (select sum(CurrentNumber) from SectionsClasses where ClassID=@pClassID and SectionID in (select vItem from split(@pSectionID,',')))

SET @vCurrentCapacity=@vCapacity-@vCurrentStudent


    IF  EXISTS (SELECT 'true' from SectionsClasses where ClassID!=@pClassID and SectionID in (select vItem from split(@pSectionID,',')))
    BEGIN
        IF @vCntr >  @vCurrentCapacity
        BEGIN
            print('Operation cannot complete; the number of student exceeds capacity')
            return
        END
    END


DECLARE FetchStudent Cursor  SCROLL for
select vItem ,rank() OVER (order by FirstName )AS Sorted from split(@pUserID,',') INNER JOIN Users ON UserID=vItem

DECLARE @vUserID  int
DECLARE @vSecID int
DECLARE @vUI int
--Used to CHECK Capacity



        --BEGIN TRAN
        Open FetchStudent
        Fetch NEXT From FetchStudent into @vUserID,@vUI
        WHILE  @@FETCH_STATUS = 0
        BEGIN 
            --Section Cursor
            DECLARE FetchSection Cursor  SCROLL for
            select vItem 
            from split(@pSectionID,',') 
            INNER JOIN SectionsClasses ON SectionsClasses.ClassID=@pClassID AND SectionsClasses.SectionID=vItem
                        WHERE CurrentNumber < Capacity

                OPEN FetchSection
                    Fetch NEXT From FetchSection into @vSecID
                    WHILE @@FETCH_STATUS = 0
                    BEGIN

                    print('#####')
                    PRINT(@vUserID)
                    PRINT(@vSecID)
                    PRINT('userID is ' + cast(@vUI as varchar ))
                    print('#####')

                    --Increase current numer of student in section by 1
                    UPDATE SectionsClasses SET CurrentNumber = CurrentNumber+1
                    WHERE SectionID=@vSecID and ClassID=@pClassID

                    Fetch NEXT From FetchSection into @vSecID
                    break
                    END
                    Close FetchSection
                    Deallocate FetchSection
        Fetch NEXT From FetchStudent into @vUserID,@vUI
        END

この結果を得るには、それを変更する必要があります

@pUserID='1,2,3,4' および @pSectionID='1,2' を渡す場合

結果は 1 1 2 2 3 1 4 2 です。. .

誰でも助けることができますか??

4

0 に答える 0