0

我有这个过程,我想修改它以选择给定参数不为空的行

Create PROC ViewclassCourses
(
@pClassID int = null,
@pcourseID int = null,
@pMinMark decimal(5,2)=null,
@pMaxMark decimal(5,2)=null
)
as

select ClassID,CourseID,minmark,maxmark from ClassCourses
Go

例如,如果我输入 ViewclassCourses @pclassID=1,@pMaxMark=100 它就像

select ClassID,CourseID,minmark,maxmark from ClassCourses where @pclassID=1 and    @pMaxMark=100'
4

2 回答 2

0

@pMaxMark 创建 PROC ViewclassCourses (@pClassID int = null, @pcourseID int = null, @pMinMark decimal(5,2)=null, @pMaxMark decimal(5,2)=null) 为

 SELECT ClassID,CourseID,minmark,maxmark FROM ClassCourses WHERE @pClassID IS NOT NULL AND @pcourseID IS NOT NULL 
Go
于 2013-10-01T13:45:35.697 回答
0

我认为您只想在参数非空时才使用它们?在我的工作中,我们通常这样做:

 Create PROC ViewclassCourses
    (
    @pClassID int = null,
    @pcourseID int = null,
    @pMinMark decimal(5,2)=null,
    @pMaxMark decimal(5,2)=null
    )
    as

    select ClassID,CourseID,minmark,maxmark from ClassCourses
    where ClassId = coalesce(@pClassID, ClassId)
    and CourseID = coalesce(@pcourseID , CourseID)
    and minmark = coalesce(@pMinMark , minmark)
    and maxmark = coalesce(@pMaxMark , maxmark)
    Go

你也可以写where @pClassId is null or ClassId = @pClassId。我们刚刚采用了“COALESCE模式”。

于 2013-10-01T13:46:39.353 回答