我有这个 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 。. .
谁能帮忙??