为Dynamic Native SQL查询的结果执行结果行

时间:2016-10-11 23:48:07

标签: sql oracle dynamic native

我正在精神上这个。我对动态SQL很新,所以我可能不会向谷歌提出正确的问题,但这就是我正在尝试做的事情......我有一个动态SQL查询。当我运行该查询时,它会产生几行。所有这些行(大约30个)组成一个联合查询。我可以复制所有这些行并粘贴到一个新的查询并运行 - 工作正常,但我需要做的是在一个查询中运行这一切。我已经查找了使用execute immediate和fetch的示例,但我似乎无法让它们实际吐出数据......他们最终会说“执行成功”,但实际上并没有产生任何结果行。以下SQL的结果列名称是“qry_txt” - 而不是以面值生成它,我想将其作为查询执行。同样,我可能不会很好地表达这一点,但我基本上试图将2个查询(涉及手动复制/粘贴步骤)转换为单个查询。希望这是有道理的......

这是我的SQL:

Select CASE when 
lead(ROWNUM) over(order by ROWNUM) is null then
'SELECT '||''''||T.TABLE_NAME||''''||' as TABLE_NAME,'||''''||T.COLUMN_NAME||''''||' as COLUMN_NAME, cast('|| T.COLUMN_NAME ||' as 
varchar2(100)) as SAMPLE_DATA ||
from rpt.'||T.TABLE_NAME ||' where '||T.COLUMN_NAME||' is not null and ROWNUM=1;'  
else
'SELECT '||''''||T.TABLE_NAME||''''||' as TABLE_NAME,'||''''||T.COLUMN_NAME||''''||' as COLUMN_NAME, cast('|| T.COLUMN_NAME ||' as 
varchar2(100)) as SAMPLE_DATA from rpt.'||T.TABLE_NAME ||' where '||T.COLUMN_NAME||' is not null and ROWNUM=1 union ' end as qry_txt
from all_tab_columns t where T.OWNER='rpt' and T.DATA_TYPE != 'BLOB' and T.DATA_TYPE != 'LONG' and T.TABLE_NAME = 'NME_DMN' 
ORDER BY ROWNUM asc;

1 个答案:

答案 0 :(得分:1)

您无法在SQL中编写动态查询。你需要使用PLSQL块来实现这一点。请看看你怎么做。 PS:代码未经过测试。

declare
  var1 <decalration same of column in select list> ;
  var2 <decalration same of column in select list> ;
  var3 <decalration same of column in select list> ;
  ....
  varn   ;

 begin
  for i in ( SELECT LEAD (ROWNUM) OVER (ORDER BY ROWNUM) COl1
              FROM all_tab_columns t
               WHERE     T.OWNER = 'rpt'
                     AND T.DATA_TYPE != 'BLOB'
                     AND T.DATA_TYPE != 'LONG'
                     AND T.TABLE_NAME = 'NME_DMN'
             ORDER BY ROWNUM ASC)

  Loop

     If i.col1 IS NULL Then

     execute immediate   'SELECT '
                           || ''''
                           || T.TABLE_NAME
                           || ''''
                           || ' as TABLE_NAME,'
                           || ''''
                           || T.COLUMN_NAME
                           || ''''
                           || ' as COLUMN_NAME, cast('
                           || T.COLUMN_NAME
                           || ' as 
                              varchar2(100)) as SAMPLE_DATA ||
                            from rpt.'
                           || T.TABLE_NAME
                           || ' where '
                           || T.COLUMN_NAME
                           || ' is not null and ROWNUM=1' into var1 , var2 ,var3 ....varn;

    Else

       execute immediate  'SELECT '
                           || ''''
                           || T.TABLE_NAME
                           || ''''
                           || ' as TABLE_NAME,'
                           || ''''
                           || T.COLUMN_NAME
                           || ''''
                           || ' as COLUMN_NAME, cast('
                           || T.COLUMN_NAME
                           || ' as 
            varchar2(100)) as SAMPLE_DATA from rpt.'
                           || T.TABLE_NAME
                           || ' where '
                           || T.COLUMN_NAME
                           || ' is not null and ROWNUM=1' into var1 , var2 ,var3 ....varn;

    end if;

  End Loop;             

  exception
   when others then
   dbms_output.put_lin(sqlcode ||'--'||sqlerrm);

End;