我在跟踪 SQL 查询时遇到问题.. 当我在没有 where 条件的情况下执行此查询时,它工作正常......但是当我使用 where 条件时它不起作用.. 它给出以下错误消息..
'Msg 137, Level 15, State 2, Line 4 必须声明标量变量“@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
还有一件事..当我使用 where 条件时
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,'')
它可以工作,但没有过滤..所以我必须以上述方式使用 where 条件......请帮助我解决这个问题......谢谢