Skip to content

Instantly share code, notes, and snippets.

@brccabral
Created December 11, 2024 00:07
Show Gist options
  • Save brccabral/9405452787aa6a574a5c4c478817fab5 to your computer and use it in GitHub Desktop.
Save brccabral/9405452787aa6a574a5c4c478817fab5 to your computer and use it in GitHub Desktop.
SQL Custom Script

SQL Custom Script

Set the environment variable DB_PASS. It will briefly create a temp file, execute it and load the password as PSSQL variable. The file is deleted.
Then, connect to the DB and run custom.sql.

set DB_PASS=secret
sqlplus /nolog @run_custom.sql
-- in "Command Prompt"
-- set DB_PASS=secret
-- sqlplus /nolog @run_custom.sql
host echo define DB_PASS=%DB_PASS% > db_pass.sql
@@db_pass.sql
host del DB_PASS.sql /q
whenever sqlerror exit sql.sqlcode;
define USERNAME='dummy'
define TNS_STRING = '@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=example.com)(PORT=1234)(LOAD_BALANCE=YES|NO))(CONNECT_DATA=(SERVICE_NAME=db_service)))'
define CONNECT_STRING=&USERNAME./"&DB_PASS"&TNS_STRING;
PROMPT Connecting as &USERNAME&TNS_STRING;
connect &CONNECT_STRING
-- output as csv
-- set markup csv on
-- doesn't show how many lines at the end of the file
-- set feedback off
-- don't print results to terminal
-- set term OFF
@@custom.sql
/
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment