Are you still using SQL*Plus?
SQL*Plus has been around since before I started with Oracle, but there is a better solution in SQLcl
. I have blogged about it before and use it for most of my ad-hoc interaction with Oracle databases. How come people don’t invest more time to get a better worklife? Just the command history itself is reason enough to switch to SQLcl. Today SQLcl is included in many Oracle installations, still I see people using SQL*Plus instead.
SQLcl is with only a few exceptions compatible with SQLplus, and when you look at the first chapters in the documentation you get the sensation that this looks familiar. Talking about the documentation, it does not explain everything you can do, some nice features are almost hidden until you go through the reference, like the MDB command to import Microsoft Access databases (see below ). But there are plenty of other blogposts on this tool, this url should return a few.
This blogpost was written when release 26.3.1.0 was current. This link always has the latest version, and you can easily check for a new version from a script that calls the binary, see Check for new release when you start SQLcl
I use SQLcl from many places and like the possibility to set up my environment by calling an URL. SQLplus has supported this for ages, but it does not handle TLS, and who does not use HTTPS these days? With SQLcl you can execute start https://example.com/yourenvironment or just @https://example.com/yourenvironment. Obviously you have to implement the tiny HTTP-server that returns the environment you have saved with the STORE command, but it takes only a few seconds with any AI to get the Python code you need, and you probably want to hide it behind a reverse-proxy, so ask your AI to help you with the NGINX configuration as well1. Setting up a good environment is a good investment for future use. At the very least I like to add this to the file:
alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss';
Just note that if you save the environment from the most current version, loading the environment on an older version may result in errors for unknown commands, but you can ignore that.
Also creating environments for different situations is handy; CSV files in Norway are often not exactly that, they have semi-colon separated values. NLS settings also varies, so I need one environment for loading a dataset from Norway and another from the big outside.
Import Access database
Do you have sensitive data in an Access database? It is not uncommon; I know about highly sensitive data still being stored in an Access database file, and shared between many on a shared drive. No access control and audit after you get hold of the file!
Please, just put it in an Oracle database, and lock it down with audit enabled. Migration of Access database was supported in SQL Developer up to around version 4, but not after2. With SQLcl it is trivial.
I googled for an available Access database file, and found one
(via
). For this test I used a user with only the db_developer_role in 26ai FREE version database.
You can list the tables in a file with this command:
mdb list -file "CD Collection.mdb"
TABLE_NAME |TABLE_TYPE
-------------------------
Disk |TABLE
Read me |TABLE
Set |TABLE
Track |TABLE
Query all |VIEW
query by artist |VIEW
query by title |VIEW
You copy the content with:
mdb copy -file "CD Collection.mdb"
In other load operations oracle calculates stats on imported tables, but not here:
exec dbms_stats.gather_schema_stats(user)
select table_name,num_rows from user_tables order by 1;
TABLE_NAME NUM_ROWS
______________ ___________
DISK 5
READ_ME 18
SET_ 3
TRACK 8
VIEWS_TABLE 5
I thought table name SET_ was just a strange side effect of the import procedure and tried to rename it:
rename table set_ to set;
But got this error, which demonstrates a rather new feature, error messages are now followed by a helpfull link:
Error starting at line : 1 in command -
rename table set_ to set
Error report -
ORA-00903: invalid table name
Help: https://docs.oracle.com/error-help/db/ora-00903/
This feature is also available in recent versions of SQL*Plus (but have you upgraded yet? You should
). The feature can be turned off with set errordetails off
Clicking on the provided URL
you’ll see that in this case the explanation is at the end: …and cannot be a reserved word. It would be nice if the dynamic view V$RESERVED_WORDS were generally available to normal users, but if you check this as system or a user with SELECT_CATALOG_ROLE role or the SELECT ANY DICTIONARY privilege, you’ll see it is a reserved word that cannot be used as an identifier. Sorry for the detour.
The VIEWS_TABLE has the definitions of the views in the Access database, it has the query name and query behind each. You can translate it into an oracle view with a litle help from your AI.
See MDB in chapter 14 for more details.
Check for new release when you start SQLcl
Given that the latest version is always available at https://download.oracle.com/otn_software/java/sqldeveloper/sqlcl-latest.zip
you can always check for a new version every time you start SQLcl by calling a script. Use curl -sI to fetch only the HTTP headers of the URL, and compare the ETag header with the latest you saved to disk. Different ETag means new version. Sometimes my script downloads a new version before it has been published on oracle.com. I have the following script in $ORACLE/.local/bin/sql (and of course $ORACLE/.local/bin is the first directory in my $PATH variable):
#!/bin/bash -e
etagfile=$HOME/.sqlcletag
download_url="https://download.oracle.com/otn_software/java/sqldeveloper/sqlcl-latest.zip"
tmpdir=$HOME/tmp
zipfile=${tmpdir}/sqlcl-latest.zip
parentdir=$HOME/Oracle
if [ ! -r $etagfile ] ; then
installed_etag="X"
else
installed_etag=$(cat $etagfile)
fi
latest_etag=$(curl -sI $download_url | grep ETag | cut -f2 -d' ' )
if [ $installed_etag != $latest_etag ] ; then
echo "Newer SQLcl exists, downloading"
# Remove previous download and backup
rm -rf $zipfile ${parentdir}/sqlcl.1
if [ -r ${parentdir}/sqlcl ] ; then
# Create a backup
mv ${parentdir}/sqlcl{,.1}
fi
curl -s -o $zipfile $download_url
unzip -d $parentdir $zipfile
echo $latest_etag > $etagfile
else
echo "Latest version already installed"
fi
${parentdir}/sqlcl/bin/sql $@
Since version 7.68, curl has had options to save the ETag to a file and compare it for you, but there have been some bugs on older versions and it does not work with all HTTP servers.
I think I learned this from a blogpost by Jeff or Kris.
A few quick tips at the end
- Use
!bashto go to a bash shell. Just!works in SQLplus and used to work in SQLcl, but in SQLcl it now just returns immediately. This on UNIX, thehostcommand works the same way. - Ctrl-R executes the SQL statement you are working on. It can be frustrating to hit enter and you just get a new line. No need to go to the end of command and hit enter, just hit Ctrl-R
- Tab complete is useful when you doubt the syntax. Write part of the SQL you know and see what it suggests. It also suggests possible table_name when you start writing part of it.
keymap listhelp setlists a lot of configurations that may be new to you. Likehelp set statusbarwhich again refers toshow statusbarwith all the options. Useful to show the database your are connect to and more. Tryset statusbar username dbid timing cursorrepeat 42 10repeats last SQL command 42 times with an interval of 10 seconds between each. Useful for DBAs that need to monitor stuff likev$session.ddl someobjectis useful to get the DDL for an object you need to recreate somewhere else, or perhaps recreate with a small change. (Like when some tool created all VARCHAR2-columns with length 4000).- Of course
descworks, butinfoandinfo+has more information like columns, indexes, and statistics.
If you host this environment file externally you may need to set a proxy in order to reach it from your database server. Set the environment variable
JAVA_TOOL_OPTIONSlike this:export JAVA_TOOL_OPTIONS="-Dhttps.proxyHost=youproxyhost.example.com -Dhttps.proxyPort=8080"This works on newer versions of SQLcl. SQLcl will confirm with Picked up JAVA_TOOL_OPTIONS: … ↩︎I think this was because some JDBC-ODBC bridge support was removed in Java 8 and is no longer included in later version of Java. ↩︎
