来自进展4GL大师的任何建议,看看下面的4GL代码块,看看它是否过度完成?可以这样做,以便更容易遵循/更具可读性吗?
我有TblA
Report
6998077
6998078
6998097
7062816
和TblB
+-----------+------------+-----------+
| ID | Source| Report |
+-----------+------------+-----------+
| 4976117 | 6998077 | 6998077 |
| 4976118 | 6998078 | 6998078 |
| 4976137 | 6998097 | 6998097 |
| 5107798 | 7062816 | 6998078 |
| 5107799 | 7062816 | 6998097 |
+-----------+------------+-----------+
在我看来,我有一个复杂的Progress 4GL循环:
def temp-table TblTemp no-undo
field cTypeOfRec as char
field Report as int
field Source as int
field ID as int
index key is unique primary Report Source ID.
procedure SOOptimize:
output stream dOut1 to value("/OutPut.txt").
export stream dOut1 delimiter "|"
"Report"
"Source"
"ID".
for each TblA no-lock
on error undo, return error on stop undo, return error:
for each TblB no-lock where
TblB.Source = TblA.Report
on error undo, return error on stop undo, return error:
find TblTemp exclusive-lock where
TblTemp.SrcPltSeq = TblA.Report and
TblTemp.RptPltSeq = TblB.Report and
TblTemp.ID = TblB.ID
no-error.
if NOT available TblTemp
then do:
create TblTemp.
assign
TblTemp.cTypeOfRec = "From LoopA"
TblTemp.SrcPltSeq = TblA.Report
TblTemp.RptPltSeq = TblB.Report
TblTemp.ID = TblB.ID.
end.
end.
for each TblB no-lock where
TblB.Report = TblA.Report
on error undo, return error on stop undo, return error:
find TblTemp exclusive-lock where
TblTemp.SrcPltSeq = TblB.Source and
TblTemp.RptPltSeq = TblA.Report and
TblTemp.ID = TblB.ID
no-error.
if NOT available TblTemp
then do:
create TblTemp.
assign
TblTemp.cTypeOfRec = "From LoopB"
TblTemp.SrcPltSeq = TblB.Source
TblTemp.RptPltSeq = TblA.Report
TblTemp.ID = TblB.PltSrcSeq.
end.
end.
end.
for each TblTemp no-lock
on error undo, return error on stop undo, return error:
export stream dOut1 delimiter "|"
TblTemp.
end.
end procedure.
然后我的进度代码的输出是:
+------------+---------+---------+---------+
| cTypeOfRec | Source | Report | ID |
+------------+---------+---------+---------+
| From LoopA | 6998077 | 6998077 | 4976117 |
| From LoopA | 6998078 | 6998078 | 4976118 |
| From LoopB | 7062816 | 6998078 | 5107798 |
| From LoopA | 6998097 | 6998097 | 4976137 |
| From LoopB | 7062816 | 6998097 | 5107799 |
+------------+---------+---------+---------+
我对Progress 4GL的知识非常有限。这段代码似乎过度了吗?可以更简单吗?
我来自SQL背景。所以在SQL中,我可以相当快速轻松地解决这个问题。我的意思是,所有这个阻止进度的代码基本上只是在说,#34;来自LoopA"如果在LoopB中为NULL,则在LoopB"
中说"这是我提出的SQL等价物:
Select
case when B.ID is null then 'From LoopA'
else B.cTypeOfRec
End "cTypeOfRec"
, A.*
from #TblTemp A
left join (
select A.*, 'From LoopB' "cTypeOfRec" from ( select * from #TblTemp)A
left join (
select B.Source, A.Report, B.ID from #TblA A
Inner join #TblB B
on B.Report=A.Report)B
on A.Source = B.Report
where B.Source is null) B
on A.Report=B.Report
and a.ID = b.ID
and a.Source= b.Source
order by A.Report
, case when B.ID is null then 'From LoopA'
else B.cTypeOfRec
End
来自进展4GL大师的任何建议,看看上面的4GL代码块,看看它是否过度完成?可以这样做,以便更容易遵循/阅读?
但我对任何答案持开放态度,如果这是在4GL中编码以获得最终结果的正确方法,那么我很好。
谢谢谢谢
答案 0 :(得分:1)
你似乎要经历两次TblA和TblB。相反,为每个TblB创建一个TblTemp。然后通过TblA查看它是否与TblTemp记录匹配。您可以将临时表逻辑移出到自己的过程。
def temp-table TblTemp no-undo
field cTypeOfRec as char
field RptPltSeq as int
field SrcPltSeq as int
field ID as int
index key is unique primary RptPltSeq SrcPltSeq ID.
define stream dOut1.
run SOOptimize.
procedure SOOptimize:
output stream dOut1 to value("OutPut.txt").
export stream dOut1 delimiter "|"
"Report"
"Source"
"ID".
for each TblB no-lock:
run updateTempRec (input "From LoopB", input TblB.Source, input TblB.Report, input TblB.ID).
end.
for each TblA no-lock:
run updateTempRec (input "From LoopA", input TblA.Report, input TblA.Report, input 0).
end.
for each TblTemp no-lock:
export stream dOut1 delimiter "|"
TblTemp.
end.
end procedure.
procedure updateTempRec:
define input parameter pcType as character no-undo.
define input parameter piSrc as integer no-undo.
define input parameter piRpt as integer no-undo.
define input parameter piID as integer no-undo.
find first TblTemp where
TblTemp.SrcPltSeq = piSrc and
TblTemp.RptPltSeq = piRpt
no-error.
if available(TblTemp) then
TblTemp.cTypeOfRec = pcType.
else
if piID <> 0 then
do:
create TblTemp.
assign
TblTemp.cTypeOfRec = pcType
TblTemp.SrcPltSeq = piSrc
TblTemp.RptPltSeq = piRpt
TblTemp.ID = piID.
end.
end procedure.
答案 1 :(得分:0)
你可以写下面的内容
for each TblA no-lock,
each TblB no-lock where
TblB.Source = TblA.Report
or TblB.Report = TblA.Report
on error undo, return error on stop undo, return error:
/* ... */
end.
我不知道这是否已经有所帮助,即。如果你可以跳过临时表。至少对于一个TblA记录,您不应该两次看到相同的TblB记录(对于具有TblB.Source = TblB.Report
的记录会发生这种情况。
如果我将TblA.Report
替换为TblB.Source
中的for each TblB
,将TblA.Report
替换为TblB.Report
,那么唯一的差异似乎是TblTemp.cTypeOfRec
和{{1}所以你应该能够缩短代码。