我在Microsoft SQL Server中编写了一个存储过程,应该从4Matrix导入结果并将它们插入到MyPortal上的nginx.conf
表中。它确实导入了结果,但其中一些是不正确的。
以下是存储过程Results
的代码:
ImportResults
过程'Get4MResult'的代码部分如下(如果此过程导致问题):
USE [MyPortal]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: CURRICULUM\r.richards
-- Create date: 24/10/2017
-- Description: Imports Selected Subject Results From 4Matrix into MyPortal
-- =============================================
ALTER PROCEDURE [dbo].[ImportResults]
@SubjectID int,
@ResultSet varchar(50)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @StudentID int
DECLARE @Upper int = (SELECT COUNT(*) FROM Students)
DECLARE @ResultSetID int
DECLARE @ResultValue NVARCHAR(50)
DECLARE @Pointer int = 1
WHILE @Pointer <= @Upper
BEGIN
SET @StudentID = (SELECT studentID
FROM
(SELECT
ROW_NUMBER() OVER (ORDER BY Students.studentLastName) AS rownumber,
studentID
FROM
Students) AS foo
WHERE rownumber = @Pointer)
SET @ResultSetID = (SELECT rsID
FROM ResultSets
WHERE (rsName = @ResultSet))
EXEC Get4MResult @StudentID, @SubjectID, @ResultSet, @Result = @ResultValue OUTPUT
INSERT INTO Results(resultSet, resultStudent, resultSubject, resultValue)
VALUES(@ResultSetID, @StudentID, @SubjectID, @ResultValue)
SET @Pointer = @Pointer + 1
END
END
理论上,只有拥有4M身份证的学生(来自学生表)才能有结果。只有KS3和KS4学生拥有4Matrix ID,因此在运行时,结果表中应该只有大约400个结果。
当我运行脚本USE [MyPortal]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: CURRICULUM\r.richards
-- Create date: 24/10/2017
-- Description: Obtains Selected Result from 4Matrix Interface
-- =============================================
ALTER PROCEDURE [dbo].[Get4MResult]
@StudentID int,
@SubjectID int,
@ResultSet varchar(50),
@Result nvarchar(50) OUTPUT
AS
BEGIN
SET NOCOUNT ON;
DECLARE @KeyStage INT
DECLARE @4MPupilID INT
DECLARE @4MSubjectID INT
DECLARE @4MSeriesID INT
DECLARE @YearGroup VARCHAR(50) = (SELECT MyPortal.dbo.Students.studentYear
FROM MyPortal.dbo.Students
WHERE(Students.studentID = @StudentID))
IF(@YearGroup = 'Year 10' OR @YearGroup = 'Year 11')
SET @KeyStage = 4
ELSE
SET @KeyStage = 3
SET @4MPupilID = (SELECT FOUR.dbo.Pupils.PupilID
FROM FOUR.dbo.Pupils
WHERE MIS = @StudentID)
IF(@KeyStage = 4)
SET @4MSubjectID = (SELECT MyPortal.dbo.Subjects.subjectQsiKs4
FROM MyPortal.dbo.Subjects
WHERE (MyPortal.dbo.Subjects.subjectID = @SubjectID))
ELSE
SET @4MSubjectID = (SELECT MyPortal.dbo.Subjects.subjectQsiKs3
FROM MyPortal.dbo.Subjects
WHERE (MyPortal.dbo.Subjects.subjectID = @SubjectID))
SET @4MSeriesID = (SELECT FOUR.dbo.Series.SeriesID
FROM FOUR.dbo.Series
WHERE (SeriesName = @YearGroup+' - '+@ResultSet))
SELECT @Result = Result
FROM FOUR.dbo.Results
WHERE (PupilID = @4MPupilID
AND SubjectID = @4MSubjectID
AND SeriesID = @4MSeriesID)
END
时,它 导入如下结果:
然而,取代我期待的约400个结果,有1080个结果条目。它们不是重复的,但我发现有些学生的结果不存在,而且我不确定脚本出错的原因。看这里:
这名学生(4292)据说在这个科目中有'A *'。但是,经过进一步检查,我发现这名学生不在KS3或KS4中,因此没有4Matrix ID。所以我不确定“A *”来自哪里。
此学生根本不应导入不的结果。
我已经查看了两个部分的代码,但看不到错误发生的位置?
答案 0 :(得分:0)
我设法解决了这个问题:
这个问题似乎出现在原来的 ImportResults 程序中,即使学生不有4Matrix ID,他们仍然会被传递ID为0或NULL,返回A *值。
我更正了以下程序:
DECLARE @StudentID int
DECLARE @Upper int = (SELECT COUNT(*) FROM Students)
DECLARE @ResultSetID int
DECLARE @ResultValue NVARCHAR(50)
DECLARE @Pointer int = 1
DECLARE @4MatrixID int
WHILE @Pointer <= @Upper
BEGIN
SET @StudentID =(SELECT studentID FROM (
SELECT
ROW_NUMBER() OVER (ORDER BY
Students.studentLastName) AS rownumber,
studentID
FROM Students
) AS foo
WHERE rownumber = @Pointer)
SET @4MatrixID = (SELECT student4mID FROM Students WHERE(studentID =
@StudentID))
IF(@4MatrixID IS NULL) <===== ###'ADDED THIS TO ELIMINATE STUDENTS WITHOUT IDs'###
GOTO nextStudent <===== ###'SENDS TO THE END OF THE SCRIPT' [1]###
SET @ResultSetID = (SELECT rsID FROM ResultSets WHERE(rsName =
@ResultSet))
EXEC Get4MResult @StudentID,@SubjectID,@ResultSet,@Result = @ResultValue
OUTPUT
INSERT INTO Results(resultSet, resultStudent, resultSubject, resultValue)
VALUES(@ResultSetID,@StudentID,@SubjectID,@ResultValue)
nextStudent: <===[1]= ###'GETS PUSHED HERE AND THEREFORE BYPASSES THE RESULT RETRIEVAL'###
SET @Pointer = @Pointer + 1
END
这意味着必须使用可怕的GOTO语句,但它现在可以正常工作! :)