When I’m troubleshooting, a common workflow for me is
- extract a problematic SQL statement from V$SQL or V$SQLSTATS or an AWR report.
- drop the SQL text into SQL Developer or VS Code so that I can format it into a more readable format.
That was starting to frustrate me so I thought: “Why not have a SQL Formatter right there inside the database?”
So I headed over to https://github.com/vertical-blank/sql-formatter where you can grab a Java implementation of the popular https://www.npmjs.com/package/sql-formatter.
(For 23ai, I could have used the Javascript implementation directly using our native engine but using the Java version let me load it into earlier versions as well).
Once I’d done “mvn clean install”, I simply loaded by resultant jar file into the database, and put a PL/SQL wrapper around it.
loadjava -user scott/tiger sql-formatter-2.0.5.jar
SQL> create or replace function format_sql(p_sqlin varchar2)
2 return varchar2 is
3 language java name 'com.github.vertical_blank.sqlformatter.SqlFormatter.format(java.lang.String) return java.lang.String';
4 /
Function created.
And voila! A SQL formatter right there in the database
SQL> select format_sql('select * from table1') txt from dual;
TXT
----------------------------------------------------------------------
select
*
from
table1




Got some thoughts? Leave a comment