需求:根据动态条件过滤源表数据,插入到业务表
DECLARE
tenantid_val NUMERIC;
BEGIN
-- 开始
tenantid_val = 106;
raise notice 'tenantid_val:%', tenantid_val;
-- 创建局部临时表
CREATE LOCAL TEMPORARY TABLE IF NOT EXISTS 临时表 (orgcode VARCHAR2(38), tenantid NUMERIC);
-- 清空临时表
DELETE FROM 临时表;
-- 插入临时表
INSERT INTO 临时表(orgcode, tenantid) SELECT Code, DECODE(substr(Code, 1, 4),
'0010', 101,
'0060', 106
) FROM XXX;
-- 创建业务表
create table if not exists YYY
(
orgCode VARCHAR2(38),
TenantID numeric
);
-- 清空业务表
delete from YYY;
-- 插入业务表
INSERT INTO YYY(
orgCode,
TenantID
)
SELECT
ZZZ_orgCode,
tenantid_val,
FROM ZZZ
WHERE EXISTS (SELECT 1 FROM 临时表 WHERE ZZZ.ZZZ_orgCode=临时表.orgcode AND 临时表.tenantid = tenantid_val);
-- 结束
END;