我已經寫了這是工作的罰款與最值的,但一些價值它給我的錯誤作爲SQL Server過程:錯誤與SQL Server過程動態
必須聲明標量變量「@CourseID 」。
我沒有太多的程序經驗,請幫忙,另請參閱參數定義和參數順序。
ALTER PROCEDURE [dbo].[prStudentLoadByStudentAtt] 86
@StID int
AS
DECLARE @SemID int
DECLARE @CourseID int
DECLARE @SQLQuery as nvarchar(4000)
DECLARE @ParamDefinition as nvarchar(4000)
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
set @SQLQuery = 'SELECT distinct dbo.tbSubjects.SubjectName,dbo.tbSubjects.SubCode, dbo.tbSubjects.SemID, dbo.tbStudent.StudentID, dbo.tbStudent.Name, dbo.tbStudent.RollNo, dbo.tbStudent.RegNo, dbo.tbAttendanceMaster.SubID, dbo.fnTotalDays(dbo.tbAttendanceMaster.SubID, dbo.tbStudent.StudentID) AS Total,
dbo.fnNoOfDays(dbo.tbAttendanceMaster.SubID, dbo.tbStudent.StudentID) AS Present, dbo.fnPerDays(dbo.tbAttendanceMaster.SubID, dbo.tbStudent.StudentID)
AS Percentage, ISNULL(dbo.tbSemester.SemName, ''Not Available'') AS SemName
FROM dbo.tbAttendance INNER JOIN dbo.tbAttendanceMaster ON dbo.tbAttendance.MasterAID = dbo.tbAttendanceMaster.AtdID INNER JOIN dbo.tbStudent ON dbo.tbAttendance.StID = dbo.tbStudent.StudentID INNER JOIN dbo.tbSubjects ON dbo.tbAttendanceMaster.SubID = dbo.tbSubjects.SubID LEFT OUTER JOIN dbo.tbSemester ON dbo.tbSubjects.SemID = dbo.tbSemester.SemID
WHERE [email protected] '
SELECT @CourseID = CourseID FROM tbStudent WHERE StudentID = @StID
SELECT @SemID = SemID FROM tbStudent WHERE StudentID = @StID
IF (ISNULL(@SemID, 0) = 0)
BEGIN
set @[email protected]+' and [email protected] '
end
else
begin
set @[email protected]+' and [email protected] '
end
set @ParamDefinition = '@SemID int, @StID int'
Execute sp_Executesql @SQLQuery, @ParamDefinition, @SemID, @StID
return
END
是參數的這兩行重要與否 – 2014-09-03 19:30:20
是的,你的第一個語句定義它們的順序必須是一樣的,你通過他們在第二順序的順序。 – Laurence 2014-09-03 19:43:52
但是在過程中,StID在semid之前定義,但在paramdefinition中,semid在stid之前出現,在sp_excutesql – 2014-09-03 20:17:12