输入和输出临时表
将临时表传入和传出程序,过程和函数非常简单。
如果你希望过程处理的数据量大于你可以轻松存储在字符串或类似数据中的数据,这可能很方便。你可以将 temp-table 作为 INPUT
,OUTPUT
和 INPUT-OUTPUT
数据传递。
输入一个临时表并输出另一个临时表:
DEFINE TEMP-TABLE ttRequest NO-UNDO
FIELD fieldA AS CHARACTER
FIELD fieldB AS CHARACTER.
/* Define a temp-table with the same fields and indices */
DEFINE TEMP-TABLE ttResponse NO-UNDO LIKE ttRequest.
/* A procedure that simply swap the values of fieldA and fieldB */
PROCEDURE swapFields:
DEFINE INPUT PARAMETER TABLE FOR ttRequest.
DEFINE OUTPUT PARAMETER TABLE FOR ttResponse.
FOR EACH ttRequest:
CREATE ttResponse.
ASSIGN
ttResponse.fieldA = ttRequest.fieldB
ttResponse.fieldB = ttRequest.fieldA.
END.
END PROCEDURE.
CREATE ttRequest.
ASSIGN ttRequest.fieldA = "A"
ttRequest.fieldB = "B".
CREATE ttRequest.
ASSIGN ttRequest.fieldA = "B"
ttRequest.fieldB = "C".
CREATE ttRequest.
ASSIGN ttRequest.fieldA = "C"
ttRequest.fieldB = "D".
/* Call the procedure */
RUN swapFields ( INPUT TABLE ttRequest
, OUTPUT TABLE ttResponse).
FOR EACH ttResponse:
DISPLAY ttResponse.
END.
结果:
fieldA--------fieldB--------
B A
C B
D C
输入输出临时表:
DEFINE TEMP-TABLE ttCalculate NO-UNDO
FIELD num1 AS INTEGER
FIELD num2 AS INTEGER
FIELD response AS DECIMAL.
PROCEDURE pythagoras:
DEFINE INPUT-OUTPUT PARAMETER TABLE FOR ttCalculate.
FOR EACH ttCalculate:
ttCalculate.response = SQRT( EXP(num1, 2) + EXP(num2, 2)).
END.
END PROCEDURE.
CREATE ttCalculate.
ASSIGN ttCalculate.num1 = 3
ttCalculate.num2 = 4.
CREATE ttCalculate.
ASSIGN ttCalculate.num1 = 6
ttCalculate.num2 = 8.
CREATE ttCalculate.
ASSIGN ttCalculate.num1 = 12
ttCalculate.num2 = 16.
/* Call the procedure */
RUN pythagoras ( INPUT-OUTPUT TABLE ttCalculate ).
FOR EACH ttCalculate:
DISPLAY ttCalculate.
END.
结果:
----------num1-- ----------num2-- -------response-
3 4 5.00
6 8 10.00
12 16 20.00
传递给功能
DEFINE TEMP-TABLE ttNumbers NO-UNDO
FIELD num1 AS INTEGER
FIELD num2 AS INTEGER
INDEX index1 num1 num2.
DEFINE VARIABLE iNum AS INTEGER NO-UNDO.
/* Forward declare the function */
FUNCTION hasAPair RETURNS LOGICAL (INPUT TABLE ttNumbers) FORWARD.
DO iNum = 1 TO 100:
CREATE ttNumbers.
ASSIGN ttNumbers.num1 = RANDOM(1,100)
ttNumbers.num2 = RANDOM(1,100).
END.
MESSAGE hasAPair(INPUT TABLE ttNumbers) VIEW-AS ALERT-BOX.
/* Function to check if two records has the same value in num1 and num2 */
FUNCTION hasAPair RETURNS LOGICAL (INPUT TABLE ttNumbers):
FIND FIRST ttNumbers WHERE ttNumbers.num1 = ttNumbers.num2 NO-ERROR.
IF AVAILABLE ttNumbers THEN
RETURN TRUE.
ELSE
RETURN FALSE.
END FUNCTION.
传递给程序文件
你可以将 temp-tables 传递给其他 .p-programs,也可以将其传递给其他过程。唯一的区别是调用程序和被调用程序必须具有相同的临时表声明。一种简单的方法是将临时表程序存储在第三个文件中 - 包含在两个程序中使用的包含。
包含临时表定义的文件:/ * ttFile.i * / DEFINE TEMP-TABLE ttFile NO-UNDO FIELD fName AS CHARACTER FORMAT“x(20)
”FIELD isADirectory AS LOGICAL。
程序检查临时表中的所有文件。他们是目录吗?
/* checkFiles.p */
{ttFile.i}
DEFINE INPUT-OUTPUT PARAMETER TABLE FOR ttFile.
FOR EACH ttFile:
FILE-INFO:FILE-NAME = ttFile.fName.
IF FILE-INFO:FILE-TYPE BEGINS "D" THEN
ttFile.isADirectory = TRUE.
END.
主程序:
{ttFile.i}
CREATE ttFile.
ASSIGN ttFile.fname = "c:\temp\".
CREATE ttFile.
ASSIGN ttFile.fname = "c:\Windows\".
CREATE ttFile.
ASSIGN ttFile.fname = "c:\Windoose\".
RUN checkFiles.p(INPUT-OUTPUT TABLE ttFile).
FOR EACH ttFile:
DISPLAY ttFile.
END.
结果:
fName----------------- isADirector
c:\temp\ yes
c:\Windows\ yes
c:\Windoose\ no