案例在哪里类 - 存储过程

时间:2015-08-26 10:10:32

标签: sql-server

我已退回以下SP

ALTER PROCEDURE [dbo].[GetStudentsByUserId]
    @userId UNIQUEIDENTIFIER,
    @roleType bit
AS
SET NOCOUNT ON;
    DECLARE @classId UNIQUEIDENTIFIER

    EXEC [dbo].[GetClassId] @userId,@classId OUTPUT

    SELECT 
        B.StudentId, 
        B.StudentName, 
        B.Description, 
        B.StudentType
    FROM 
        ClassStudents A
    INNER JOIN 
        Student B
    ON 
        B.StudentId = A.StudentId 
    WHERE 
        A.CassId = @classId
    AND 
    ( 
        B.StudentType =
        CASE 
            @roleType
            when 1 
            then 1
            when 2
            then 2
        END
    OR 
        B.StudentType =
        CASE 
            @roleType
            when 1
            then 4
            when 2
            then 2
        END
    )

当我按照以下

运行时
exec [GetStudentsByUserId] '28f95465-f30f-4b8b-cc92-d4c4e8d73273', 1

它返回学生类型为1的学生。 当我按照以下方式运行时

exec [GetStudentsByUserId] '28f95465-f30f-4b8b-cc92-d4c4e8d73273', 2

此外,它还会返回学生类型为1的学生。但我需要2型学生。

当我将roleType作为1传递给roleType时,我希望得到第1类,第4类学生,当我将create table #itemsInShippment (itemid int, shippmentid int, qty int) insert into #itemsInShippment (itemid, shippmentid, qty) SELECT 10 as itemid, 1 as shippmentid, 100 as qty UNION SELECT 20 , 1, 200 UNION SELECT 10 , 2, 300 UNION SELECT 10 , 3, 1000 CREATE TABLE #shippments (shippmentid int , dt date, shippmentstatus varchar(50), supplierid int) insert into #shippments (shippmentid, dt, shippmentstatus,supplierid) SELECT 1 as shippmentid, '2015-01-12' as dt, 'OK' as shippmentstatus , 5000 as supplierid UNION ALL SELECT 2, '2015-01-17', 'OK' , 5000 UNION ALL SELECt 3, '2015-01-17' , 'Cancelled' , 5000 SELECT cte.* FROM ( select a.itemid,b.dt,a.qty, (coalesce( SUM(case when shippmentstatus <> 'Cancelled' then a.qty else 0 end) OVER (PARTITION BY a.itemid) ,0) - coalesce( SUM(case when shippmentstatus <> 'Cancelled' then a.qty else 0 end) OVER (PARTITION BY a.itemid,a.shippmentid) ,0) ) AS qtyInOtherShipments from #itemsInShippment a left join #shippments b on a.shippmentid = b.shippmentid where b.supplierid = 5000 --and shippmentstatus = 'Cancelled' ) as cte where cte.dt='2015-01-12' 作为2传递时,我希望得到第2类学生。

任何更好的解决方案都非常值得赞赏。

1 个答案:

答案 0 :(得分:0)

无法将2传递到位类型。你需要改变你的where子句,如下所示。

ALTER PROCEDURE [dbo].[GetStudentsByUserId]
        @userId UNIQUEIDENTIFIER,
        @roleType bit
    AS
    SET NOCOUNT ON;
        DECLARE @classId UNIQUEIDENTIFIER

        EXEC [dbo].[GetClassId] @userId,@classId OUTPUT

        SELECT 
            B.StudentId, 
            B.StudentName, 
            B.Description, 
            B.StudentType
        FROM 
            ClassStudents A
        INNER JOIN 
            Student B
        ON 
            B.StudentId = A.StudentId 
        WHERE 
            A.CassId = @classId
        AND 
        ( 
                   (B.StudentType in (2) and 
            and      @roleType=1)
        OR 
            (B.StudentType in (1,4) and 
            and      @roleType=0)
        )