这篇文章主要讲解了"在SQL PLUS中格式化输出查询结果的方法是什么",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"在SQL PLUS中格式化输出查询结果的方法是什么"吧!
一:过程代码
creatoreplayprocedurat _ SqL(I _ SqLinvarchar 2)为
i _ CursorIntegerDefaultDbms _ Sql .open _ Cursor();
Desc .desc _ Tab;
i _ StatusInteger-执行状态
n _ ColcntNumber-列数
I _ Col _ width整数:=3;-列间距,如果觉得太小可以调大一些
n _ row cntnumber :=0;-记录结果行数
VC _ columnnamevarchar 2(4000);-列头名字
VC _ Col _ splitvarchar 2(4000):='-';-记录和列头之间的分割线
VC _ column valuevarchar 2(4000);-列值
typ _ collenglistablefnumberindexbybinary _ Integer;
tab _ Typ _ Collength Typ _ Collength;-存放每一列值的最大长度
l _ ResultsLong-输出结果
开始
-第一次循环获取每列值的最大长度
i_Cursor:=Dbms_Sql .打开光标-打开游标;
Dbms_Sql .解析(i_Cursor,i_Sql,Dbms_Sql .原生);
Dbms_Sql .描述列(光标,列,Desc列);
ForiIn1.n_ColcntLoop
Dbms_Sql .Define_Column(i_Cursor,I,Vc_Columnvalue,4000);
tab _ Typ _ Collength(I):=长度(desc)科尔斯.col _ Name);
EndLoop
i_Status:=Dbms_Sql .执行(I _ Cursor);
环
ExitWhenDbms _ Sql .fetch _ Rows(I _ Cursor)1;
ForiIn1.n_ColcntLoop
Dbms_Sql .列值(i_Cursor,I,Vc _列值);
如果长度(Vc_Columnvalue)选项卡_类型_收集长度则
Tab_Typ_Collength(i) := Length(Vc_Columnvalue);
End If;
End Loop;
End Loop;
Dbms_Sql.Close_Cursor(i_Cursor);
--第二次循环拼接输出结果
i_Cursor := Dbms_Sql.Open_Cursor; --打开游标;
Dbms_Sql.Parse(i_Cursor, i_Sql, Dbms_Sql.Native);
Dbms_Sql.Describe_Columns(i_Cursor, n_Colcnt, Desc_Cols);
For i In 1 .. n_Colcnt Loop
Dbms_Sql.Define_Column(i_Cursor, i, Vc_Columnvalue, 4000);
End Loop;
i_Status := Dbms_Sql.Execute(i_Cursor);
Loop
Exit When Dbms_Sql.Fetch_Rows(i_Cursor) < 1;
n_Rowcnt := n_Rowcnt + 1;
l_Results := l_Results || Rpad(n_Rowcnt, i_Col_Width + 3);
For i In 1 .. n_Colcnt Loop
Dbms_Sql.Column_Value(i_Cursor, i, Vc_Columnvalue);
l_Results := l_Results ||
Rpad(Vc_Columnvalue || ' ',
Tab_Typ_Collength(i) + i_Col_Width);
End Loop;
l_Results := l_Results || Chr(10);
End Loop;
--定义列头
Vc_Columnname := Rpad('NO', i_Col_Width + 3, ' ');
For i In 1 .. n_Colcnt Loop
Vc_Columnname := Vc_Columnname ||
Rpad(Desc_Cols(i).Col_Name,
Tab_Typ_Collength(i) + i_Col_Width,
' ');
Vc_Col_Split := Rpad(Vc_Col_Split,
Length(Vc_Col_Split) + i_Col_Width,
' ');
Vc_Col_Split := Rpad(Vc_Col_Split,
Length(Vc_Col_Split) + Tab_Typ_Collength(i),
'-');
End Loop;
Dbms_Sql.Close_Cursor(i_Cursor);
-- Vc_Columnname := Vc_Columnname || Chr(10);
--输出结果
Dbms_Output.Put_Line('Total Row: ' || n_Rowcnt);
Dbms_Output.Put_Line(Vc_Columnname);
Dbms_Output.Put_Line(Vc_Col_Split);
Dbms_Output.Put_Line(l_Results);
Exception
When Others Then
Dbms_Sql.Close_Cursor(i_Cursor);
Raise;
End;
二:测试结果
TS@PROD> set serveroutput on size 999999 TS@PROD> select sql_text,sql_id,child_number,hash_value from v$sql Where sql_text Like'select * from emp%'; SQL_TEXT --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- SQL_ID CHILD_NUMBER HASH_VALUE ------------- ------------ ---------- select * from emp a2dk8bdn0ujx7 0 1745700775 select * from emp a2dk8bdn0ujx7 1 1745700775 select * from emp e,dept d where e.deptno = d.deptno 4cs33ya9vumkh 0 2478657104 TS@PROD> exec Format_Sql('select sql_text,sql_id,child_number,hash_value from v$sql Where sql_text Like''select * from emp%'''); Total Row: 3 NO SQL_TEXT SQL_ID CHILD_NUMBER HASH_VALUE --- ---------------------------------------------------- ------------- ------------ ---------- 1 select * from emp a2dk8bdn0ujx7 0 1745700775 2 select * from emp a2dk8bdn0ujx7 1 1745700775 3 select * from emp e,dept d where e.deptno = d.deptno 4cs33ya9vumkh 0 2478657104 PL/SQL procedure successfully completed.
感谢各位的阅读,以上就是“在SQL PLUS中格式化输出查询结果的方法是什么”的内容了,经过本文的学习后,相信大家对在SQL PLUS中格式化输出查询结果的方法是什么这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!
内容来源网络,如有侵权,联系删除,本文地址:https://www.230890.com/zhan/68678.html