oracle动态查询通过sql获取游标变量

时间:2014-07-08 15:24:31

标签: sql database oracle plsql

需要的帮助很少。我有一个动态查询,输出4个列名和两个表名到6个游标变量。现在我需要使用游标变量来选择前4列,然后使用游标变量从两个表名中选择,因为那些包含数据的东西通过查询使用包含查询但我不知道的变量进行查询该如何去做。这就是我现在所需要的,我只需要获取光标变量并将下摆变为查询

DECLARE
arow            VARCHAR2 (1000);

column1         VARCHAR2 (50);
column2         VARCHAR2 (50);
column3         VARCHAR2 (50);
column4         VARCHAR2 (50);
table1          VARCHAR2 (50);
table2          VARCHAR2 (50);
match           VARCHAR2 (50);
match1          VARCHAR2 (50);
sql_statement   VARCHAR2 (500);
BEGIN
FOR arow IN (SELECT   column_name_old,
                     column_name_new,
                     column_name_old_2,
                     column_name_new_2,
                     table_name_old,
                     table_name_new
              FROM   A550003.META_DATA_TABLE)
LOOP
  sql_statement :=
        'INSERT'
     || ' '
     || 'INTO'
     || ' '
     || 'a550003.MATCH_TABLE'
     || ' '
     || 'SELECT '
     || arow.column_name_old
     || ', '
     || arow.column_name_new
     || ', '
     || 'DECODE( '
     || arow.column_name_old
     || ', '
     || arow.column_name_new
     || ','
     || '1'
     || ','
     || '0)'
     || 'AS'
     || ' '
     || 'MATCH'
     || ','
     || arow.column_name_old_2
     || ', '
     || arow.column_name_new_2
     || ','
     || 'DECODE( '
     || arow.column_name_old_2
     || ', '
     || arow.column_name_new_2
     || ','
     || '1'
     || ','
     || '0)'
     || 'AS'
     || ' '
     || 'MATCH1'
     || ' FROM '
     ||' '
     || arow.table_name_old
     || ', '
     || arow.table_name_new
     || ' WHERE '
     || arow.column_name_old
     || '='
     || arow.column_name_new
     || '(+)';
  DBMS_OUTPUT.PUT_LINE (sql_statement);

 EXECUTE IMMEDIATE sql_statement;

  COMMIT;
END LOOP;
END;

2 个答案:

答案 0 :(得分:0)

首先你的sql_statement是错误的。您应该将此变量的值设置为这样(当您获得所有需要的名称时):

sql_statement := 'SELECT '
                 || column1 
                 || ', ' 
                 || column2 
                 || ', ' 
                 || column3 
                 || ', ' 
                 || column4 
                 || ' FROM ' 
                 || table1
                 || ', '
                 || table2
                 || ' WHERE '
                 || -- JOIN_CONDITION

然后你可以使用EXECUTE IMMEDIATE语句:

EXECUTE IMMEDIATE sql_statement
             INTO col1_val
                 ,col2_val
                 ,col3_val
                 ,col4_val
                 ;

当然必须声明变量col [1..4] _val。

还要注意SQL注入。

答案 1 :(得分:0)

一定是这样的:

sql_statement := 'SELECT ' 
    || column1 || ', ' || column2 || ', ' || column3 || ', ' || column4 
    || ' FROM ' || table1 || ', ' || table2 
    || ' WHERE ' || column1 ||'=' ||column2||'(+)'; 
EXECUTE IMMEDIATE sql_statement INTO col1_val, col2_val, col3_val, col4_val;

或者最好

sql_statement := 'SELECT ' 
    || column1 || ', ' || column2 || ', ' || column3 || ', ' || column4 
    ||' FROM '||table1||' LEFT OUTER JOIN '||table2||' ON '||column1||'='||column2;
EXECUTE IMMEDIATE sql_statement INTO col1_val, col2_val, col3_val, col4_val;

对于调试DBMS_OUTPUT.PUT_LINE (sql_statement);将是有用的!

然后你不需要Ref-Cursor,只需执行:

BEGIN
   For aRow in (SELECT * FROM a550003.meta_data_table) LOOP
      sql_statement := 'SELECT '||aRow.column1||','||aRow.column2 ...
   END LOOP;
END;