strong text``Hello All,
I have newly started with Oracle and trying write one script for below problem statement.
Problem Statement : I want to perform reconciliation for Two table Table A and Table B. based on fields present in Mapping Table with flag 1. In this case I am using Dynamic SQL to handle runtime column and join conditions but my query is not giving any output. Please give me solution or else any other method via I can perform this Dynamic Reconciliation in Oracle?
----------------------Reconciliation Query------------------------ **Note- Below query is running without any error but not giving any output. I don't want to use Cursor to print the result because I have Million Records. ** --code
DECLARE
v_sql VARCHAR2(32767);
v_columns VARCHAR2(32767);
v_join_condition VARCHAR2(32767);
v_column_list SYS.ODCIVARCHAR2LIST;
v_result SYS_REFCURSOR;
BEGIN
SELECT LISTAGG(Column_name, ',') WITHIN GROUP (ORDER BY Column_name)
INTO v_columns
FROM MappIng_table
WHERE Flag = 1;
-- Split the column list into individual column names
v_column_list := SYS.ODCIVARCHAR2LIST();
v_column_list.EXTEND(REGEXP_COUNT(v_columns, ',') + 1);
FOR i IN 1..v_column_list.COUNT LOOP
v_column_list(i) := REGEXP_SUBSTR(v_columns, '[^,]+', 1, i);
END LOOP;
-- Build the join condition dynamically
v_join_condition := '';
FOR i IN 1..v_column_list.COUNT LOOP
IF i > 1 THEN
v_join_condition := v_join_condition || ' AND ';
END IF;
v_join_condition := v_join_condition || 'a.' || v_column_list(i) ||
'=b.' || v_column_list(i);
END LOOP;
v_sql := 'SELECT * FROM Table_A a full outer JOIN Table_B b ON ' ||
v_join_condition;
dbms_output.put_line(v_sql);
EXECUTE IMMEDIATE v_sql;
END;