在oracle中执行动态sql

问题描述 投票:0回答:1
With DYN_DateCheck As
(Select 'Select ' || ''''|| TABLE_NAME ||'''' || ' As Tbl, ' || ''''|| COLUMN_NAME||'''' || ' As Fld, ' || 'Check_Date(' || COLUMN_NAME || ', :dateformat) As ValidDate ' || 
         'From '   || TABLE_NAME  || '_TST ' ||
         'Where '  || COLUMN_NAME || ' IS NOT NULL UNION'
  From  all_tab_columns 
  Where NOT OWNER  In ('SYS', 'XDB', 'GSMADMIN_INTERNAL' ) 
  And TABLE_NAME   In ('EGL_CLIENTDETAIL', 'STG_PLY', 'STG_PLR', 'STG_IAC', 'STG_PPD')
  And   DATA_TYPE   = 'DATE'
  UNION
  Select 'Select ' || '''XYZ''' || ' As Tbl, ' || '''XYZ''' || ' As Fld, NULL As ValidDate From Dual' From Dual
  Order By 1
)
Select * From (Select * From DYN_DateCheck)

您可能已经知道这个最终的 Select 语句实际上不会执行 DYN_DateCheck 中的所有 Selects / Unions - 它只是提供动态 SQL。如何执行 DYN_DateCheck 中的动态 Select 语句?

我尝试过立即执行 - 我不太明白,但我不知道如何继续使用已执行的内容。我需要使用此动态 sql 运行时的结果。

oracle
1个回答
0
投票

一个简单的选择是复制该语句的结果,将其粘贴(在 SQL*Plus 提示符下或粘贴到您可能正在使用的 GUI 工具中),然后执行它,例如

SQL>     With DYN_DateCheck As
  2      (Select 'Select ' || ''''|| TABLE_NAME ||'''' || ' As Tbl, ' || ''''|| COLUMN_NAME||'''' || ' As Fld, ' || 'Check_Date(' || COLUMN_NAME || ', :dateformat) As ValidDate ' ||
  3               'From '   || TABLE_NAME  || '_TST ' ||
  4               'Where '  || COLUMN_NAME || ' IS NOT NULL UNION'
  5        From  all_tab_columns
  6        Where NOT OWNER  In ('SYS', 'XDB', 'GSMADMIN_INTERNAL' )
  7        And TABLE_NAME   In ('EGL_CLIENTDETAIL', 'STG_PLY', 'STG_PLR', 'STG_IAC', 'STG_PPD')
  8        And   DATA_TYPE   = 'DATE'
  9        UNION
 10        Select 'Select ' || '''XYZ''' || ' As Tbl, ' || '''XYZ''' || ' As Fld, NULL As ValidDate From Dual' From Dual
 11        Order By 1
 12      )
 13      Select * from dyn_datecheck;

'SELECT'||''''||TABLE_NAME||''''||'ASTBL,'||''''||COLUMN_NAME||''''||'ASFLD,'||'
--------------------------------------------------------------------------------
Select 'XYZ' As Tbl, 'XYZ' As Fld, NULL As ValidDate From Dual

SQL> Select 'XYZ' As Tbl, 'XYZ' As Fld, NULL As ValidDate From Dual;

TBL FLD V
--- --- -
XYZ XYZ

SQL>

如果您想自动化(某种程度),请将该语句转换为返回 refcursor 的函数:

SQL> create or replace function f_test
  2    return sys_refcursor
  3  is
  4    l_query varchar2(4000);
  5    rc sys_Refcursor;
  6  begin
  7      With DYN_DateCheck As
  8      (Select 'Select ' || ''''|| TABLE_NAME ||'''' || ' As Tbl, ' || ''''|| COLUMN_NAME||'''' || ' As Fld, ' || 'Check_Date(' || COLUMN_NAME || ', :dateformat) As ValidDate ' ||
  9               'From '   || TABLE_NAME  || '_TST ' ||
 10               'Where '  || COLUMN_NAME || ' IS NOT NULL UNION'
 11        From  all_tab_columns
 12        Where NOT OWNER  In ('SYS', 'XDB', 'GSMADMIN_INTERNAL' )
 13        And TABLE_NAME   In ('EGL_CLIENTDETAIL', 'STG_PLY', 'STG_PLR', 'STG_IAC', 'STG_PPD')
 14        And   DATA_TYPE   = 'DATE'
 15        UNION
 16        Select 'Select ' || '''XYZ''' || ' As Tbl, ' || '''XYZ''' || ' As Fld, NULL As ValidDate From Dual' From Dual
 17        Order By 1
 18      )
 19      Select *
 20      into l_query
 21      From DYN_DateCheck;
 22
 23    open rc for l_query;
 24    return rc;
 25  end;
 26  /

Function created.

然后只需调用该函数即可:

SQL> select f_test from dual;

F_TEST
--------------------
CURSOR STATEMENT : 1

CURSOR STATEMENT : 1

TBL FLD V
--- --- -
XYZ XYZ


SQL>
© www.soinside.com 2019 - 2024. All rights reserved.