[20211123]完善expand sql text.txt
[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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- [20170619]11G expand sql text.txtSQL
- [20170620]11G 12c expand sql text.txtSQL
- oracle hint_no_expand_no_factOracle
- sql tuning set/sql tuning advisor(待完善)SQL
- [20170726]11G 12c expand sql text 2.txtSQL
- vimscript-expand函式詳解函式
- [20131128]12c的dbms_utility.expand_sql_text.txtSQL
- expand 與 unexpand 命令例項教程
- awrsqrpt.sql使用方法,功能持續完善中SQL
- [20210407]完善ti.sql指令碼.txtSQL指令碼
- [20231128]完善ashtable.sql.txtSQL
- 核對主備庫表的資料sql語句(待完善)SQL
- [20211123]sqlplus @與@@的區別.txtSQL
- [20220510]完善tpt expandz.sql指令碼.txtSQL指令碼
- [20211230]完善sql_id指令碼.txtSQL指令碼
- [20221010]完善descz.sql指令碼.txtSQL指令碼
- [20221101]完善descz.sql指令碼.txtSQL指令碼
- [20221101]完善gts.sql指令碼.txtSQL指令碼
- [20211130]完善tpt t.sql指令碼.txtSQL指令碼
- [20211122]完善descx.sql指令碼.txtSQL指令碼
- [20230414]完善seg2.sql指令碼.txtSQL指令碼
- [20231117]完善ashtt.sql指令碼.txtSQL指令碼
- [20230203]完善awr.sql指令碼.txtSQL指令碼
- Linux基礎命令—格式轉換expand、unexpandLinux
- [20221012]完善spsw.sql指令碼.txtSQL指令碼
- [20221208]完善bind_cap.sql指令碼.txtSQL指令碼
- [20211129]完善tpt tablist.sql指令碼.txtSQL指令碼
- [20211202]完善d_buffer.sql指令碼.txtSQL指令碼
- [20211129]完善tpt killi.sql指令碼.txtSQL指令碼
- [20220323]完善tpt get_trace.sql指令碼.txtSQL指令碼
- [20220309]完善shp4.sql指令碼.txtSQL指令碼
- [20220217]完善tpt gts.sql指令碼.txtSQL指令碼
- [20230210]建立完善swcnm.sql指令碼.txtSQL指令碼
- [20230123]完善curheapz.sql指令碼.txtSQL指令碼
- [20210125]完善hide.sql指令碼.txtIDESQL指令碼
- Linux基礎命令---文字格式轉換expand、unexpandLinux
- [20220823]完善tpt的ashtop.sql指令碼.txtSQL指令碼
- [20231025]完善tpt的trans.sql指令碼.txtSQL指令碼