sql-serverpivotpivotviewer

Where condition in sql PIVOT


I have a problem in following SQL query.. When I am executing this Query without where condition it works fine... but when I use where condition it doesnt work.. it gives following error message..

'Msg 137, Level 15, State 2, Line 4 Must declare the scalar variable "@courseid1".'

ALTER PROCEDURE [dbo].[SP_Attendance]
    @courseid as int=null, @subjid int=null
AS

Declare @colList varchar(max)
Declare @qry varchar(max)

Declare @courseid1 as int=@courseid, 
@subjid1 int=@subjid

SET @colList = STUFF((SELECT distinct ',' + QUOTENAME(SA.Attend_Date)
            FROM Student_Attendance_Sheet SA
            FOR XML PATH(''), TYPE
            ).value('/', 'NVARCHAR(MAX)') 
        ,1,1,'')

SET @qry = 'SELECT Roll_No, EnrollmentNo, STUD_FNAME + STUD_MNAME + STUD_LNAME as [Student Name] , '+@colList+'
FROM (
    select SA.Roll_No, SA.Attend_Date, SA.Attendance from Student_Attendance_Sheet SA
    where (SA.Course_ID = +@courseid1+) and (SA.Subject_ID =  +@subjid1+) 
) as s
PIVOT
(
    MAX(Attendance)
    FOR Attend_Date IN (' + @colList + ')
) pvt '
print(@qry)
Exec(@qry)

exec SP_Attendance 2, 3

One more thing.. when I am using where condition in

SET @colList = STUFF((SELECT distinct ',' + QUOTENAME(SA.Attend_Date)
            FROM Student_Attendance_Sheet SA
where (SA.Course_ID = +@courseid1+) and (SA.Subject_ID =  +@subjid1+) 
            FOR XML PATH(''), TYPE
            ).value('/', 'NVARCHAR(MAX)') 
        ,1,1,'')

It works but there is no filtering.. so I have to use where condition in above mentioned manner... Please help me for solving this problem... Thanks


Solution

  • I suggest you.. don't create More variables unnecessarily.. it occupies ur memory space You just go with...

    ALTER PROCEDURE [dbo].[SP_Attendance]
        @courseid as int=null, @subjid int=null
    AS
    
    Declare @colList varchar(max)
    Declare @qry varchar(max)
    
    SET @colList = STUFF((SELECT distinct ',' + QUOTENAME(SA.Attend_Date)
                FROM Student_Attendance_Sheet SA
                where Course_ID= @courseid AND Subject_ID = @subjid
                FOR XML PATH(''), TYPE
                ).value('/', 'NVARCHAR(MAX)') 
            ,1,1,'')
    
    SET @qry = 'SELECT Roll_No, EnrollmentNo, STUD_FNAME + STUD_MNAME + STUD_LNAME as [Student Name] , '+@colList+'
    FROM (
        select Roll_No, EnrollmentNo, STUD_FNAME, STUD_MNAME, STUD_LNAME, Attend_Date, Attendance from Student_Attendance_Sheet
        where (Course_ID = '+cast(@courseid as varchar(50))+ ') and (Subject_ID =  '+cast(@subjid as varchar(50))+ ') 
    ) as s
    PIVOT
    (
        MAX(Attendance)
        FOR Attend_Date IN (' + @colList + ')
    ) pvt '
    print(@qry)
    Exec(@qry)
    

    Try this..