如何在 RPGLE 的一行中将不同的字段移动或合并到子文件中
How to MOVE or MERGE different fields into the subfile in ONE LINE in RPGLE
我对如何在一行中移动或显示来自不同字段的不同值感到困惑。
我的输出应该是这样的
Real Output
但现在,我的输出是这样的
Recent Output
这是我的实体文件
CUREXG file
我在物理文件中有三个字段,它们是:
- EXGDAT = 日期和关键字段
- EXGCOD = 兑换码
- EXGRAT = 汇率
我有 2 个日期,基本上我需要输出只有 2 行,其中一行是 5 月 31 日,第二行是 6 月 1 日。
我试图通过执行 if 条件来对它们进行分组,但它没有用。我该怎么办?请帮助我
提前致谢
//Add a logical for the table by date, exchange code
fcurexg2 if e k disk
**---------------------- start of your code
*LOVAL setll curexg
read curexg
dou %eof(curexg);
c eval @@date = exgdat
c exsr $GetVals
eval rrn = rrn + 1
write sfl01
// move to the next date
exgdat setgt curexg
read curexg
enddo
**------------------------
Begsr $GetVals; // runs for each code -- usd, eur, etc
@@gcod = 'USD'
exsr $GetGrat;
move @@grat USD
@@gcod = 'GBP'
exsr $GetGrat;
move @@grat GBP
@@gcod = 'EUR'
exsr $GetGrat;
move @@grat EUR
@@gcod = 'AUD'
exsr $GetGrat;
move @@grat AUD
@@gcod = 'SGD'
exsr $GetGrat;
move @@grat SGD
Endsr;
**------------------------
Begsr $GetGrat; //find the rate for that date and code
*like define curexg @@date
*like define exgcod @@gcod
*like define exgrat @@grat
clear @@grat
Chain (@@date: @@gcod) curexg2; //the new logical
if %found(curexg2);
@@grat = exgrat
endif
Endsr;
**------------------------
考虑一个 SQL 函数。这是一个 SQL 函数,其中 returns 特定兑换代码和日期的汇率。
CREATE or replace function curexg_exchangeRate(
inDate date,
inCurrency char(3))
returns decimal(7,2)
language sql
begin
declare Sqlcode int ;
declare vSqlcode DECIMAL(5,0) default 0 ;
declare vExgrat decimal(7,2) default 0 ;
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
SET vSqlcode = SQLCODE ;
select a.exgrat
into vExgrat
from curexg a
where a.exgdat <= inDate
and a.exgcod = inCurrency
order by a.exgdat desc
fetch first row only ;
return coalesce( vExgrat, 0 ) ;
end
调用 exchangeRate sql 函数的 RPG 代码:
d usdRate s 7p 2
d gbpRate s 7p 2
d eurRate s 7p 2
/free
// get exchange rate, of each exchange code, as of the specified date
exec sql
set :usdRate = curexg_exchangeRate( :exgdat, 'USD' ) ;
exec sql
set :gbpRate = curexg_exchangeRate( :exgdat, 'GBP' ) ;
exec sql
set :eurRate = curexg_exchangeRate( :exgdat, 'EUR' ) ;
此代码读取每个兑换日期的汇率:
// list exchange rates for each exchange date.
exec sql
declare c1 cursor for
with t1 as (
select distinct a.exgdat
from curexg a
order by a.exgdat )
select a.exgdat,
curexg_exchangeRate( a.exgdat, 'USD' ) usdRate,
curexg_exchangeRate( a.exgdat, 'GBP' ) gbpRate,
curexg_exchangeRate( a.exgdat, 'EUR' ) eurRate
from t1 a
order by a.exgdat ;
exec sql
open c1 ;
exec sql
fetch c1
into :exgdat, :usdRate, :gbpRate, :eurRate ;
if sqlcode <> 0 ;
leave ;
endif ;
// write to subfile
sfExgdat = exgdat ;
sfUsdRate = usdRate ;
sfGbpRate = gbpRate ;
sfEurRate = eurRate ;
write sflrcd ;
enddo ;
exec sql
close c1 ;
*inlr = '1' ;
return ;
/end-free
我对如何在一行中移动或显示来自不同字段的不同值感到困惑。
我的输出应该是这样的
Real Output
但现在,我的输出是这样的
Recent Output
这是我的实体文件
CUREXG file
我在物理文件中有三个字段,它们是:
- EXGDAT = 日期和关键字段
- EXGCOD = 兑换码
- EXGRAT = 汇率
我有 2 个日期,基本上我需要输出只有 2 行,其中一行是 5 月 31 日,第二行是 6 月 1 日。 我试图通过执行 if 条件来对它们进行分组,但它没有用。我该怎么办?请帮助我
提前致谢
//Add a logical for the table by date, exchange code
fcurexg2 if e k disk
**---------------------- start of your code
*LOVAL setll curexg
read curexg
dou %eof(curexg);
c eval @@date = exgdat
c exsr $GetVals
eval rrn = rrn + 1
write sfl01
// move to the next date
exgdat setgt curexg
read curexg
enddo
**------------------------
Begsr $GetVals; // runs for each code -- usd, eur, etc
@@gcod = 'USD'
exsr $GetGrat;
move @@grat USD
@@gcod = 'GBP'
exsr $GetGrat;
move @@grat GBP
@@gcod = 'EUR'
exsr $GetGrat;
move @@grat EUR
@@gcod = 'AUD'
exsr $GetGrat;
move @@grat AUD
@@gcod = 'SGD'
exsr $GetGrat;
move @@grat SGD
Endsr;
**------------------------
Begsr $GetGrat; //find the rate for that date and code
*like define curexg @@date
*like define exgcod @@gcod
*like define exgrat @@grat
clear @@grat
Chain (@@date: @@gcod) curexg2; //the new logical
if %found(curexg2);
@@grat = exgrat
endif
Endsr;
**------------------------
考虑一个 SQL 函数。这是一个 SQL 函数,其中 returns 特定兑换代码和日期的汇率。
CREATE or replace function curexg_exchangeRate(
inDate date,
inCurrency char(3))
returns decimal(7,2)
language sql
begin
declare Sqlcode int ;
declare vSqlcode DECIMAL(5,0) default 0 ;
declare vExgrat decimal(7,2) default 0 ;
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
SET vSqlcode = SQLCODE ;
select a.exgrat
into vExgrat
from curexg a
where a.exgdat <= inDate
and a.exgcod = inCurrency
order by a.exgdat desc
fetch first row only ;
return coalesce( vExgrat, 0 ) ;
end
调用 exchangeRate sql 函数的 RPG 代码:
d usdRate s 7p 2
d gbpRate s 7p 2
d eurRate s 7p 2
/free
// get exchange rate, of each exchange code, as of the specified date
exec sql
set :usdRate = curexg_exchangeRate( :exgdat, 'USD' ) ;
exec sql
set :gbpRate = curexg_exchangeRate( :exgdat, 'GBP' ) ;
exec sql
set :eurRate = curexg_exchangeRate( :exgdat, 'EUR' ) ;
此代码读取每个兑换日期的汇率:
// list exchange rates for each exchange date.
exec sql
declare c1 cursor for
with t1 as (
select distinct a.exgdat
from curexg a
order by a.exgdat )
select a.exgdat,
curexg_exchangeRate( a.exgdat, 'USD' ) usdRate,
curexg_exchangeRate( a.exgdat, 'GBP' ) gbpRate,
curexg_exchangeRate( a.exgdat, 'EUR' ) eurRate
from t1 a
order by a.exgdat ;
exec sql
open c1 ;
exec sql
fetch c1
into :exgdat, :usdRate, :gbpRate, :eurRate ;
if sqlcode <> 0 ;
leave ;
endif ;
// write to subfile
sfExgdat = exgdat ;
sfUsdRate = usdRate ;
sfGbpRate = gbpRate ;
sfEurRate = eurRate ;
write sflrcd ;
enddo ;
exec sql
close c1 ;
*inlr = '1' ;
return ;
/end-free