CREATE TABLE example_one
(
eo_id NUMBER
, eo_value NUMBER
);
INSERT INTO example_one(eo_id, eo_value) VALUES(1, 10);
CREATE TABLE example_two
(
et_id NUMBER
, et_value NUMBER
);
INSERT INTO example_two(et_id, et_value) VALUES(1, 20);
CREATE TABLE db_sql_statements
(
s_stmt_id NUMBER
, s_stmt VARCHAR2(4000) /* should be CLOB - it can exceed 4000 CHAR */
);
INSERT INTO db_sql_statements(s_stmt_id, s_stmt) VALUES(1,'SELECT ~[columns_list_#00001]~ FROM ~[source_tables_#00001]~ ~[ON join_conditions_#00001]~ ~[WHERE where_conditions_#00001]~');
);
CREATE TABLE db_sql_parameters
(
p_param_id VARCHAR2(4000)
, p_param VARCHAR2(4000)
);
INSERT INTO db_sql_parameters(p_param_id, p_param) VALUES('columns_list_#00001', 'eo.eo_id, eo.eo_value + et.et_value');
INSERT INTO db_sql_parameters(p_param_id, p_param) VALUES('source_tables_#00001', 'example_one eo JOIN example_two et');
INSERT INTO db_sql_parameters(p_param_id, p_param) VALUES('join_conditions_#00001', 'ON eo.eo_id = et.et_id');
INSERT INTO db_sql_parameters(p_param_id, p_param) VALUES('where_conditions_#00001', 'WHERE 1 = 1');
CREATE TABLE db_sql_stmt_param_bridge
(
pb_stmt_id NUMBER
, pb_param_r NUMBER
, pb_param_id VARCHAR2(4000)
);
INSERT INTO db_sql_stmt_param_bridge(pb_stmt_id, pb_param_r, pb_param_id) VALUES(1, 1, 'columns_list_#00001');
INSERT INTO db_sql_stmt_param_bridge(pb_stmt_id, pb_param_r, pb_param_id) VALUES(1, 2, 'source_tables_#00001');
INSERT INTO db_sql_stmt_param_bridge(pb_stmt_id, pb_param_r, pb_param_id) VALUES(1, 3, 'join_conditions_#00001');
INSERT INTO db_sql_stmt_param_bridge(pb_stmt_id, pb_param_r, pb_param_id) VALUES(1, 4, 'where_conditions_#00001');
SELECT s.s_stmt, p.p_param_id, p.p_param
FROM db_sql_statements s
JOIN db_sql_stmt_param_bridge pb
ON s.s_stmt_id = pb.pb_stmt_id
JOIN db_sql_parameters p
ON pb.pb_param_id = p.p_param_id
;
/*
SELECT ~[columns_list_#00001]~ FROM ~[source_tables_#00001]~ ~[ON join_conditions_#00001]~ ~[WHERE where_conditions_#00001]~ columns_list_#00001 eo.eo_id, eo.eo_value + et.et_value
SELECT ~[columns_list_#00001]~ FROM ~[source_tables_#00001]~ ~[ON join_conditions_#00001]~ ~[WHERE where_conditions_#00001]~ source_tables_#00001 example_one eo JOIN example_two et
SELECT ~[columns_list_#00001]~ FROM ~[source_tables_#00001]~ ~[ON join_conditions_#00001]~ ~[WHERE where_conditions_#00001]~ join_conditions_#00001 ON eo.eo_id = et.et_id
SELECT ~[columns_list_#00001]~ FROM ~[source_tables_#00001]~ ~[ON join_conditions_#00001]~ ~[WHERE where_conditions_#00001]~ where_conditions_#00001 WHERE 1 = 1
*/
SELECT REPLACE
(
REPLACE
(
REPLACE
(
REPLACE
(
s_stmt
, '~[columns_list_#00001]~'
, p_param
)
, '~[source_tables_#00001]~'
, lead_p_param
)
, '~[ON join_conditions_#00001]~'
, lead_p_param2
)
, '~[WHERE where_conditions_#00001]~'
, lead_p_param3
)
FROM
(
SELECT s.s_stmt
, p.p_param_id
, p.p_param
, LEAD(p.p_param) OVER (PARTITION BY s.s_stmt ORDER BY pb.pb_param_r) AS lead_p_param
, LEAD(p.p_param, 2) OVER (PARTITION BY s.s_stmt ORDER BY pb.pb_param_r) AS lead_p_param2
, LEAD(p.p_param, 3) OVER (PARTITION BY s.s_stmt ORDER BY pb.pb_param_r) AS lead_p_param3
, ROW_NUMBER() OVER (PARTITION BY s.s_stmt ORDER BY pb.pb_param_r) AS r_number
FROM db_sql_statements s
JOIN db_sql_stmt_param_bridge pb
ON s.s_stmt_id = pb.pb_stmt_id
JOIN db_sql_parameters p
ON pb.pb_param_id = p.p_param_id
)
WHERE r_number = 1
;
-- SELECT eo.eo_id, eo.eo_value + et.et_value FROM example_one eo JOIN example_two et ON eo.eo_id = et.et_id WHERE 1 = 1
SELECT eo.eo_id, eo.eo_value + et.et_value FROM example_one eo JOIN example_two et ON eo.eo_id = et.et_id WHERE 1 = 1;
-- 1 30
从 PL/SQL 运行它并使用 EXECUTE IMMEDIATE。我在编写数据库框架时做过类似的事情。