[20211123]完善expand sql text.txt

lfree發表於2021-11-24

[20211123]完善expand sql text.txt

--//在整理自己以前寫的一些指令碼,發現tpt下也有一個類似指令碼。

 $ cat expandlast.sql
.
-- Copyright 2020 Tanel Poder. All rights reserved. More info at
-- Licensed under the Apache License, Version 2.0. See LICENSE.txt for terms & conditions.

COL outsql FOR A100 WORD_WRAP
VAR outsql CLOB

0 c clob := q'\
0 declare

999999      \';;
999999 begin
999999     dbms_utility.expand_sql_text(c, :outsql);;
999999 end;;
/

PRINT outsql

--//11g版本要呼叫dbms_sql2.expand_sql_text。12c以上才是執行dbms_utility.expand_sql_text.
--//如果以上指令碼在11g上執行一定報錯。

--//如果修改如下,加入引數1輸入:

$ cat expandlast.sql
.
-- Copyright 2020 Tanel Poder. All rights reserved. More info at
-- Licensed under the Apache License, Version 2.0. See LICENSE.txt for terms & conditions.

COL outsql FOR A100 WORD_WRAP
VAR outsql CLOB

0 c clob := q'\
0 declare

999999      \';;
999999 begin
999999     --dbms_utility.expand_sql_text(c, :outsql);;
999999     dbms_&&1..expand_sql_text(c, :outsql);;
999999 end;;
/

PRINT outsql

--//這樣輸入引數1=sql2,就可以在11g執行,輸入引數1=utility就可以在12c以上執行。

SCOTT@book> @ ver1
PORT_STRING                    VERSION        BANNER
------------------------------ -------------- --------------------------------------------------------------------------------
x86_64/Linux 2.4.xx            11.2.0.4.0     Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production

SCOTT@book> select sysdate  from dual ;
SYSDATE
-------------------
2021-11-23 15:31:33

SCOTT@book> @ expandlast.sql sql2

PL/SQL procedure successfully completed.

OUTSQL
----------------------------------------------------------------------------------------------------
SELECT SYSDATE "SYSDATE" FROM "SYS"."DUAL" "A1"

--//很明顯要記住輸入引數sql2 ,utility 。還是很不方便。如果輸入引數11,自動變為sql2,就容易記住也方便使用。
--//我改寫如下,不行!!
$ cat expand.sql
.
-- Copyright 2020 Tanel Poder. All rights reserved. More info at
-- Licensed under the Apache License, Version 2.0. See LICENSE.txt for terms & conditions.

COL outsql FOR A100 WORD_WRAP
VAR outsql CLOB
column arg1 new_value arg1

0 c clob := q'\
0 declare

999999      \';;
0 select decode(&1,11,'sql2','utility') arg1 from dual ;;
999999 begin
999999     --dbms_utility.expand_sql_text(c, :outsql);;
999999     dbms_&&arg1..expand_sql_text(c, :outsql);;
999999 end;;
/

PRINT outsql

--//注:0 select decode(&1,11,'sql2','utility') arg1 from dual ;; 不能寫在前面,這樣輸出的是這條語句。
SCOTT@book> @ expand  11
select decode(11,11,'sql2','utility') arg1 from dual;
                                                    *
ERROR at line 1:
ORA-00911: invalid character
SP2-1504: Cannot print uninitialized LOB variable "OUTSQL"

--//呼叫ed開啟,看到內容如下:
select decode(&1,11,'sql2','utility') arg1 from dual;
declare
c clob := q'\
select * from emp
     \';
begin
    --dbms_utility.expand_sql_text(c, :outsql);
    dbms_&&arg1..expand_sql_text(c, :outsql);
end;
/

--//放棄!視乎這樣必須要在declare裡面才行,單獨儲存不報錯。我可以寫成如下:

$ cat expand.sql
.
-- Copyright 2020 Tanel Poder. All rights reserved. More info at
-- Licensed under the Apache License, Version 2.0. See LICENSE.txt for terms & conditions.

COL outsql FOR A100 WORD_WRAP
VAR outsql CLOB

0 c clob := q'\
0 declare

999999      \';;
999999 begin
999999     &&1 dbms_utility.expand_sql_text(c, :outsql);;
999999     &&2 dbms_sql2.expand_sql_text(c, :outsql);;
999999 end;;
/

PRINT outsql

--//透過控制引數1,引數2實現,比如:
SCOTT@book> select sysdate  from Dual ;
SYSDATE
-------------------
2021-11-23 17:45:21

SCOTT@book> @ expand  -- ""
PL/SQL procedure successfully completed.

OUTSQL
----------------------------------------------------------------------------------------------------
SELECT SYSDATE "SYSDATE" FROM "SYS"."DUAL" "A1"

--//不過還是不好記憶,不知道有什麼好方法實現....還是採用輸入引數帶有sql_id的方式改寫如下:

$ cat expandz.sql
set long 40000
set serveroutput on
column arg new_value arg

set term off
select decode(&2,11,'sql2','utility') arg from dual;
set term on
prompt

declare
    l_sqltext clob := null;
    l_result  clob := null;
begin
        select sql_fulltext into l_sqltext from v$sqlarea where sql_id='&&1';
        dbms_&arg..expand_sql_text(l_sqltext,l_result);
        dbms_output.put_line(l_result);
end;
/
set serveroutput off

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/267265/viewspace-2843767/,如需轉載,請註明出處,否則將追究法律責任。

相關文章