Execute Oracle SQLs from Linux Command Prompt and Exit

_____________________________________________________________________________________________________________________

The Eucharistic Miracles of the World
Method 1 
Execute single SQL command from the prompt


[oracle@TESTBOX exp]$ echo 'select count(*) from tab;' | sqlplus / as sysdba

SQL*Plus: Release 19.0.0.0.0 - Production on Fri Jul 17 07:46:47 2020
Version 19.3.0.0.0

Copyright (c) 1982, 2019, Oracle.  All rights reserved.


Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.3.0.0.0

SQL> 
  COUNT(*)
----------
      8308

SQL> Disconnected from Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.3.0.0.0


Method 2 
Execute multiple SQL statements using .sql script
$ cat test.sql
select 1 from dual;
select name from v$database; 
select count(*) from tab;

sqlplus / as sysdba @test.sql
sqlplus test/test @test.sql

To exit from the sql prompt either you have to put exit in the .sql file or use below command.

exit | sqlplus / as sysdba @test.sql

Demo:-


$ exit | sqlplus / as sysdba @test.sql

SQL*Plus: Release 19.0.0.0.0 - Production on Fri Jul 17 07:54:56 2020
Version 19.3.0.0.0

Copyright (c) 1982, 2019, Oracle.  All rights reserved.


Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.3.0.0.0


	 1
----------
	 1


NAME
---------
CDB


  COUNT(*)
----------
      8308

SQL> Disconnected from Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.3.0.0.0

Method 3

sqlplus /nolog <<EOF
connect test/test
@ test.sql 
select * from dual;
EOF

_____________________________________________________________________________________________________________________

0 comments:

Post a comment

 

acehints.com Copyright 2011-20 All Rights Reserved | Site Map | Contact | Disclaimer