我从我的数据库中获取数据,其中一个条件将始终相同,另一个条件的值将为1到13.查询必须执行13次,每个值一次。< / p>
Everything编译,但是当我查看输出时,我注意到用于从SELECT
语句中检索数据的值永远不会保留任何值。当我使用SQL Server Management Studio中的硬编码WHERE
语句执行完全相同的查询时,将按预期检索数据。
我收到的SQLCODE
是100
。
当我省略PERFORM VARYING
循环并为我的查询提供硬编码数据时,我的QUESTION-CODE
和ANSWER-CODE
字段包含正确的数据,但AMOUNT
字段首先为空,零。
我正在使用OpenCobol编译器和SQL Server 2012。
我在俯瞰什么?
01 WS-FIELDS.
05 I PIC 9(2).
01 WS-EVALUATION.
05 QUESTION-CODE PIC 9(3).
05 ANSWER-CODE PIC 9(3).
01 WS-RESULT.
05 OV PIC 9(3).
05 V PIC 9(3).
05 G PIC 9(3).
05 ZG PIC 9(3).
05 NVT PIC 9(3).
05 AMOUNT PIC 9(3).
LINKAGE SECTION.
01 ROUTE-CODE PIC X(10) VALUE SPACES.
EXEC SQL
DECLARE crs
CURSOR FOR
SELECT QuestionCode, AnswerCode, COUNT(AnswerCode)
FROM Answers
WHERE Route = :ROUTE-CODE AND QuestionCode= :I
GROUP BY QuestionCode, AnswerCode
END-EXEC
EXEC SQL
OPEN crs
END-EXEC
PERFORM UNTIL SQLCODE <> 0
PERFORM VARYING I FROM 1 BY 1 UNTIL I = 13
EXEC SQL
FETCH crs INTO :QUESTION-CODE, :ANSWER-CODE, AMOUNT
END-EXEC
IF SQLCODE = 0
EVALUATE TRUE
WHEN ANSWER-CODE = 1
MOVE AMOUNT TO OV
WHEN ANSWER-CODE = 2
MOVE AMOUNT TO V
WHEN ANSWER-CODE = 3
MOVE AMOUNT TO G
WHEN ANSWER-CODE = 4
MOVE AMOUNT TO ZG
WHEN ANSWER-CODE = 5
MOVE AMOUNT TO NVT
WHEN OTHER
DISPLAY "Error" UPON SYSOUT
END-EVALUATE
END-IF
END-PERFORM
END-PERFORM
.
答案 0 :(得分:2)
按照以下方式更改:
EXEC SQL
DECLARE crs
CURSOR FOR
SELECT QuestionCode, AnswerCode, COUNT(AnswerCode)
FROM Answers
WHERE Route = :ROUTE-CODE AND QuestionCode between 1 and 13
GROUP BY QuestionCode, AnswerCode
Order By QuestionCode
END-EXEC
EXEC SQL
OPEN crs
END-EXEC
EXEC SQL
FETCH crs
......
END-EXEC
PERFORM UNTIL SQLCODE <> 0
EXEC SQL
FETCH crs
......
END-EXEC
End-Perform
注意:您可能需要根据数据/逻辑检查是否缺少QuestionCode
同样在Cobol PERFORM UNTIL SQLCODE&lt;&gt;在大多数语言中,0实际上是一个while循环。 如果您希望在循环结束时完成测试,请使用 With Test After 选项。
备选方案,您可以删除光标并在循环中为每行添加一个选择。