SELECT concat(concat('m.', COLUMN_NAME), concat(' = mtf.', COLUMN_NAME)) A
FROM all_tab_columns
where table_name like 'T_EMPLOYEE'
如何将AND
和行聚合到varchar中作为下面的预期结果:
m.a_emp_id = mft.a_emp_id AND m.a_emp_name = mft.a_emp_name
AND m.a_emp_sal = mft.a_emp_sal AND m.a_emp_dept = mft.a_emp_dept
AND m.a_emp_status = mft.a_emp_status
答案 0 :(得分:1)
试试这个。这可能有所帮助。
-------------------------------PLSQL Starts-----------------------------------------
set serveroutput on;
DECLARE
lv_cond LONG;
BEGIN
FOR i IN (
SELECT
CASE
WHEN ROWNUM =1
THEN 'WHERE '
||concat(concat('m.', COLUMN_NAME), concat(' = mtf.', COLUMN_NAME))
ELSE 'AND '
||concat(concat('m.', COLUMN_NAME), concat(' = mtf.', COLUMN_NAME))
END col1
FROM all_tab_columns
WHERE table_name = 'EMP_V1') LOOP
lv_cond:=lv_cond||' '||i.col1;
END LOOP;
dbms_output.put_line(lv_cond);
END;
-------------------------------PLSQL Ends-----------------------------------------
SELECT
CASE
WHEN ROWNUM =1
THEN 'WHERE '
||concat(concat('m.', COLUMN_NAME), concat(' = mtf.', COLUMN_NAME))
ELSE 'AND '
||concat(concat('m.', COLUMN_NAME), concat(' = mtf.', COLUMN_NAME))
END
FROM all_tab_columns
WHERE table_name = 'EMP_V1' ;
---------------------------------OUTPUT---------------------------------------------
COL1
WHERE m.EMPNO = mtf.EMPNO
AND m.ENAME = mtf.ENAME
AND m.JOB = mtf.JOB
AND m.MGR = mtf.MGR
AND m.HIREDATE = mtf.HIREDATE
AND m.SAL = mtf.SAL
AND m.COMM = mtf.COMM
AND m.DEPTNO = mtf.DEPTNO
AND m.NEWADD = mtf.NEWADD
------------------------------------------------------------------------------------
答案 1 :(得分:1)
自11G以来,您可以使用LISTAGG功能。
SELECT LISTAGG(
'm.' || COLUMN_NAME || ' = ' || 'mtf.' || COLUMN_NAME,
CHR(13) || CHR(10) || 'AND ')
WITHIN GROUP (ORDER BY COLUMN_ID) A
FROM all_tab_columns
where table_name like 'T_EMPLOYEE'
||
进行varchar连接(对我来说看起来更干净)。CHR(13) || CHR(10)
是CRLF WITHIN GROUP (ORDER BY COLUMN_ID)
指定收集行以进行连接的顺序