我拥有的团队
源表 -->SRC_TBL as
TaxCode,ExemCode,CountryCode
TX1,EX1,US
TX1,EX2,UK
TX3,EX1,US
TX2,EX2,UK
3 规格表为 ---> SPEC_TBL_TX
TaxCode
TX1
TX2
TX4
SPEC_TBL_EX
ExmCode
EX1
EX3
EX4
SPEC_TBL_CTY
Contry
US
IN
SG
需要将每个源表值与每个规格表值中的值进行比较,并按如下方式更新目标表
TAR_TBL
TaxCode,ExemCode,CountryCode,ERROR
TX1,EX1,US,VALID
TX1,EX2,UK,INVALID EX code+ Invalid Country
TX3,EX1,US,INVALID TX Code
TX2,EX2,UK,INVALID EX code+ Invalid Country
需要用pl/sql MINUS 概念来实现。批量收集是否也需要任何 BULK COLLECT 逻辑?
可能您根本不需要 PL/SQL。请参阅以下示例,其中
temp1
将源表外连接到所有其他表,如果不匹配则返回 关键字
temp2
聚合结果(这意味着 - 如果
NULL
- 一切正常;否则,某些内容是无效
SQL> WITH
2 temp
3 AS
4 (SELECT s.taxcode,
5 s.exmcode,
6 s.countrycode,
7 CASE WHEN tx.taxcode IS NOT NULL THEN '' ELSE 'taxcode' END status
8 FROM src_tbl s LEFT JOIN spec_tbl_tx tx ON tx.taxcode = s.taxcode
9 UNION ALL
10 SELECT s.taxcode,
11 s.exmcode,
12 s.countrycode,
13 CASE WHEN ex.exmcode IS NOT NULL THEN '' ELSE 'exmcode' END status
14 FROM src_tbl s LEFT JOIN spec_tbl_ex ex ON ex.exmcode = s.exmcode
15 UNION ALL
16 SELECT s.taxcode,
17 s.exmcode,
18 s.countrycode,
19 CASE WHEN cy.countrycode IS NOT NULL THEN '' ELSE 'countrycode' END status
20 FROM src_tbl s LEFT JOIN spec_tbl_cty cy ON cy.countrycode = s.countrycode),
21 temp2
22 AS
23 ( SELECT taxcode,
24 exmcode,
25 countrycode,
26 LISTAGG (status, ', ') WITHIN GROUP (ORDER BY NULL) status
27 FROM temp
28 GROUP BY taxcode, exmcode, countrycode)
29 SELECT taxcode,
30 exmcode,
31 countrycode,
32 CASE WHEN status IS NULL THEN 'VALID' ELSE 'INVALID: ' || status END result
33 FROM temp2;
结果:
TAXCODE EXMCODE COUNTRYCODE RESULT
------- ------- --------------- ------------------------------
TX1 EX1 US VALID
TX1 EX2 UK INVALID: countrycode, exmcode
TX2 EX2 UK INVALID: countrycode, exmcode
TX3 EX1 US INVALID: taxcode
SQL>