sqlplus从所选结果中进行选择

时间:2018-09-07 09:51:26

标签: sql oracle plsql

我正在使用Oracle sqlplus,并尝试使用从下面的代码中选择的结果,然后再次从Table_1中选择该列

SELECT * 
FROM
(
SELECT Column_Name 
FROM All_Tab_columns 
WHERE Table_Name=UPPER('Table_1')
)
INTERSECT
(
SELECT Column_Name 
FROM All_Tab_columns 
WHERE Table_Name=UPPER('Table_2')
);

我该如何执行类似的操作:

SELECT <Columns_That_Intersected>
FROM Table_1;

是否可以将Columns_That_Intersected存储在变量/函数/过程中,并将其再次用于其他select语句?

3 个答案:

答案 0 :(得分:2)

是的,有可能。只需将查询的汇总结果保存在need permission to view the Server/Database state中即可。像这样:

create table items1 as 
select rownum id, 'me '||rownum name, 1 dummy
from xmlTable ('1 to 3');
create table items2 as select id, name from items1;

set verify off
col Columns_That_Intersected new_value Columns_That_Intersected noprint 

select listagg (column_name, ',') within group (order by null) Columns_That_Intersected
from (
    select column_name
    from All_Tab_Columns 
    where Table_Name=UPPER('items1')
    intersect
    select column_name
    from All_Tab_columns 
    where Table_Name=UPPER('items2')
);

prompt Columns_That_Intersected=&Columns_That_Intersected
select &Columns_That_Intersected 
from items1; 

输出:

Columns_That_Intersected=ID,NAME

        ID NAME
---------- -------------------------------------------
         1 me 1
         2 me 2
         3 me 3

NbBundle.Messages API Documentation

答案 1 :(得分:1)

仅获取表table1和table2中都存在的列名,您可以使用以下查询:

SELECT Column_Name
  FROM all_tab_columns t1
 WHERE table_name = 'Table1'
   AND EXISTS (SELECT 1
                 FROM all_tab_columns t2
                WHERE table_name = 'Table2'
                  AND t2.Column_Name = t1.Column_Name);

然后从Table1中获取这些列值,可以使用下面的PL / SQL构造:

DECLARE
v_sql_statement VARCHAR2(2000);
v_cols          VARCHAR2(2000);
BEGIN
 FOR cn IN (SELECT Column_Name
              FROM all_tab_columns t1
             WHERE table_name = 'Table1'
               AND EXISTS (SELECT 1
                             FROM all_tab_columns t2
                            WHERE table_name = 'Table2'
                              AND t2.Column_Name = t1.Column_Name))
 LOOP
    v_cols := v_cols || ', ' || cn.column_name;

 END LOOP;

 v_cols := ltrim(v_cols, ',');
 v_sql_statement := 'SELECT ' || v_cols || ' FROM Table1';
 EXECUTE IMMEDIATE v_sql_statement;

END; 

答案 2 :(得分:0)

例如,您可以使用动态sql。

您可以使用listagg函数将选择的结果汇总到列表中

SELECT listagg( Column_Name,',') WITHIN GROUP (ORDER BY 1) cols
FROM
(
  SELECT Column_Name 
  FROM All_Tab_columns 
  WHERE Table_Name=UPPER('tab1')      
  INTERSECT      
  SELECT Column_Name 
  FROM All_Tab_columns 
  WHERE Table_Name=UPPER('tab2')

);

因此,您得到例如。 col1,col2,col3 借助此列表,您可以在plsql块中创建一个游标,然后循环遍历以打印出所需的wat。

declare
  v_cols    VARCHAR2(2000);
  v_select_string varchar(2000);
  csr                SYS_REFCURSOR;
  v1            VARCHAR2(2000);
BEGIN
  SELECT listagg( Column_Name,',') WITHIN GROUP (ORDER BY 1) cols
  into v_cols
  FROM
  (
    SELECT Column_Name 
    FROM All_Tab_columns 
    WHERE Table_Name=UPPER('tab1')    
    INTERSECT    
    SELECT Column_Name 
    FROM All_Tab_columns 
    WHERE Table_Name=UPPER('tab2')  
  );

  v_select_string   := 'SELECT ' || v_cols  ||
                       ' FROM tab1';

  OPEN csr FOR v_select_string;

  LOOP
    FETCH csr INTO v1;

    EXIT WHEN csr%NOTFOUND;         

    DBMS_OUTPUT.PUT_LINE( v1);
  END LOOP;

  CLOSE csr;
END P1;