call oracle procedure in conection string - oracle

Is it possible to call an Oracle packageX. procedureY(a, b, c) from the conection string using SqlPlus?
Something like sqlplus user/pass#tns #packageX.ProcedureY(a,b,c) without the need to log into sqlplus and then execute that procedure?

Yes
Jeffreys-Mini:bin thatjeffsmith$ ./sql hr/oracle
SQLcl: Release 19.2.1 Production on Tue Aug 20 18:35:18 2019
Copyright (c) 1982, 2019, Oracle. All rights reserved.
Last Successful login time: Tue Aug 20 2019 18:35:19 -04:00
Connected to:
Oracle Database 18c Enterprise Edition Release 18.0.0.0.0 - Production
Version 18.3.0.0.0
SQL> create or replace procedure do_nothing
2 is
3 begin
4 null;
5 end do_nothing;
6 /
Procedure DO_NOTHING compiled
SQL> exit
Disconnected from Oracle Database 18c Enterprise Edition Release 18.0.0.0.0 - Production
Version 18.3.0.0.0
Jeffreys-Mini:bin thatjeffsmith$ ./sql hr/oracle <<EOF
> exec do_nothing()
> quit
> EOF
SQLcl: Release 19.2.1 Production on Tue Aug 20 18:36:14 2019
Copyright (c) 1982, 2019, Oracle. All rights reserved.
Last Successful login time: Tue Aug 20 2019 18:36:15 -04:00
Connected to:
Oracle Database 18c Enterprise Edition Release 18.0.0.0.0 - Production
Version 18.3.0.0.0
PL/SQL procedure successfully completed.
Disconnected from Oracle Database 18c Enterprise Edition Release 18.0.0.0.0 - Production
Version 18.3.0.0.0
Jeffreys-Mini:bin thatjeffsmith$

In windows you can do it as following:
echo execute packageX.ProcedureY(a,b,c)|user/pass#tns
Cheers!!

Related

How to Redirect Input to SQL*Plus without Exiting using Windows PowerShell

Using Windows PowerShell, how do I redirect input into sqlplus (non-interactive stdin mode) such that once the redirected input is complete sqlplus is left open in interactive stdin mode without SQL*Plus exiting?
It appears as though the redirected input is issuing an implicit exit which SQL*Plus is processing.
Using Oracle 19c Enterprise Edition, Oracle InstantClient 19c 64-bit, Windows PowerShell Desktop 5.1.19041.1320, Microsoft Windows 10.0 build 19042.
Thank you in advance.
dev.sql
set echo on
set serveroutput on
exec dbms_output.put_line('hello world');
No redirection executes start script dev.sql and does not exit (notice the SQL> prompt as the last line instead of PS C:\Users\my>).
PS C:\Users\my> sqlplus my_username/my_password#"my_host:my_port/my_service" "#dev.sql"
SQL*Plus: Release 19.0.0.0.0 - Production on Tue Feb 15 14:44:48 2022
Version 19.3.0.0.0
Copyright (c) 1982, 2019, Oracle. All rights reserved.
Last Successful login time: Tue Feb 15 2022 14:40:03 -05:00
Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.14.0.0.0
SQL> set serveroutput on
SQL> exec dbms_output.put_line('hello world');
hello world
PL/SQL procedure successfully completed.
SQL>
However, when redirecting the input, then SQL*Plus automatically exits (notice the PS C:\Users\my> prompt as the last line instead of SQL>). No matter how I redirect the input into sqlplus, SQL*Plus automatically exits.
Redirection with Get-Content.
PS C:\Users\my> Get-Content dev14.sql | sqlplus my_username/my_password#"my_host:my_port/my_service"
SQL*Plus: Release 19.0.0.0.0 - Production on Tue Feb 15 14:48:42 2022
Version 19.3.0.0.0
Copyright (c) 1982, 2019, Oracle. All rights reserved.
Last Successful login time: Tue Feb 15 2022 14:44:49 -05:00
Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.14.0.0.0
SQL> SQL> SQL> hello world
PL/SQL procedure successfully completed.
SQL> Disconnected from Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.14.0.0.0
PS C:\Users\my>
Redirection with here-string.
PS C:\Users\my> #"
>> set echo on
>> set serveroutput on
>> exec dbms_output.put_line('hello world');
>> "# | sqlplus my_username/my_password#"my_host:my_port/my_service"
SQL*Plus: Release 19.0.0.0.0 - Production on Tue Feb 15 14:50:14 2022
Version 19.3.0.0.0
Copyright (c) 1982, 2019, Oracle. All rights reserved.
Last Successful login time: Tue Feb 15 2022 14:48:44 -05:00
Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.14.0.0.0
SQL> SQL> SQL> hello world
PL/SQL procedure successfully completed.
SQL> Disconnected from Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.14.0.0.0
PS C:\Users\my>
Single line redirection.
PS C:\Users\my> "select * from dual;" | sqlplus my_username/my_password#"my_host:my_port/my_service"
SQL*Plus: Release 19.0.0.0.0 - Production on Tue Feb 15 15:03:10 2022
Version 19.3.0.0.0
Copyright (c) 1982, 2019, Oracle. All rights reserved.
Last Successful login time: Tue Feb 15 2022 15:00:34 -05:00
Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.14.0.0.0
SQL>
D
-
X
SQL> Disconnected from Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.14.0.0.0
PS C:\Users\my>
Redirect using Start-Process.
PS C:\Users\my> Start-Process sqlplus my_username/my_password#"my_host:my_port/my_service" -RedirectStandardInput dev.sql -NoNewWindow -Wait
SQL*Plus: Release 19.0.0.0.0 - Production on Tue Feb 15 15:06:20 2022
Version 19.3.0.0.0
Copyright (c) 1982, 2019, Oracle. All rights reserved.
Last Successful login time: Tue Feb 15 2022 15:06:07 -05:00
Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.14.0.0.0
SQL> SQL> SQL> hello world
PL/SQL procedure successfully completed.
SQL> Disconnected from Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.14.0.0.0
PS C:\Users\my>
Redirecting using batch, still causes SQL*Plus to exit.
dev.bat
(
echo set echo on
echo set serveroutput on
echo exec dbms_output.put_line('hello world'^^^);
) | sqlplus my_username/my_password#"my_host:my_port/my_service"
PS C:\Users\my> c:dev.bat
C:\Users\my>(
echo set echo on
echo set serveroutput on
echo exec dbms_output.put_line('hello world'^);
) | sqlplus my_username/my_password#"my_host:my_port/my_service"
SQL*Plus: Release 19.0.0.0.0 - Production on Tue Feb 15 15:57:51 2022
Version 19.3.0.0.0
Copyright (c) 1982, 2019, Oracle. All rights reserved.
Last Successful login time: Tue Feb 15 2022 15:57:38 -05:00
Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.14.0.0.0
SQL> SQL> SQL> hello world
PL/SQL procedure successfully completed.
SQL> Disconnected from Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.14.0.0.0
PS C:\Users\my>
Note: I tried using -noexit but that is an argument to executing powershell itself not sqlplus.
It is like sqlplus says "okay, no more stdin, I'm done". Maybe there is a way to direct stdin back to they keyboard once pipped input reaches end-of-file, so sqlplus is left waiting for the next keyboard input?
Note: It is also odd how we do not see the set serveroutput on getting echoed.
Thank you.
Sqlplus automatically exits when its' input pipe (stdin) is closed. So the only option is to do not close pipe: you can write a program that starts sqlplus and feeds it's stdin and closes it only when you want.
I don't know why do you want to leave it open in non-interactive mode.
For me it's much better to pipe to file and then execute it.
Don't know will it help you, but I have a couple of workarounds how to leave it open for some time: use host command. For example the following command will leave sqlplus opened for 15 seconds:
"host powershell Start-Sleep -s 15" | sqlplus user/pass#//host:port/service
Note that you can't do anything with interactive stdin within it, like
"host timeout /t 10" | sqlplus user/pass#//host:port/service
because
SQL> ERROR: Input redirection is not supported, exiting the process immediately.

SP2-0310: unable to open file "LOGIN.SQL" from Oracle DB 18c in Docker for Windows container

In Docker for Windows I installed an Oracle DB 18c XE Linux Docker container by the books:
Downloaded official sources (from https://github.com/oracle/docker-images)
Created an image from it
Created a container:
docker run --name $containerName -d -p 15210:1521 -e ORACLE_PWD=$dbPassword -v $dbVolume:/opt/oracle/oradata 'oracle/database:18.4.0-xe'
When I open SQL*Plus within the container, everything works fine:
> docker exec -it DevDB sqlplus sys/pwd#//localhost:1521/xe as sysdba
SQL*Plus: Release 18.0.0.0.0 - Production on Fri May 21 19:47:10 2021
Version 18.4.0.0.0
Copyright (c) 1982, 2018, Oracle. All rights reserved.
Connected to:
Oracle Database 18c Express Edition Release 18.0.0.0.0 - Production
Version 18.4.0.0.0
SQL> show con_name
CON_NAME
------------------------------
CDB$ROOT
SQL>
In my Windows host I installed SQLPlus Instant Client. When I run SQL*Plus from there, I get a warning message telling me that "LOGIN.SQL" cannot be opened.
However, I seem to be able to run queries:
> sqlplus sys/pwd#//localhost:15210/xe as sysdba
SQL*Plus: Release 18.0.0.0.0 - Production on Fri May 21 21:34:58 2021
Version 18.5.0.0.0
Copyright (c) 1982, 2018, Oracle. All rights reserved.
Connected to:
Oracle Database 18c Express Edition Release 18.0.0.0.0 - Production
Version 18.4.0.0.0
SP2-0310: unable to open file "LOGIN.SQL"
SQL> show con_name
CON_NAME
------------------------------
CDB$ROOT
SQL>
What is causing this warning message? How can I get rid of it?
After some more testing I now found the answer to my issue:
Apparently, the reported error is a glitch in the 18.5.0.0 release of SQL*Plus.
I just updated SQL*Plus to 19.11.0.0, and the warning message is gone:
> sqlplus sys/pwd#//localhost:15210/xe as sysdba
SQL*Plus: Release 19.0.0.0.0 - Production on Fri May 21 21:53:11 2021
Version 19.11.0.0.0
Copyright (c) 1982, 2020, Oracle. All rights reserved.
Connected to:
Oracle Database 18c Express Edition Release 18.0.0.0.0 - Production
Version 18.4.0.0.0
SQL> show con_name
CON_NAME
------------------------------
CDB$ROOT
SQL>

I am getting this error :ORA-12560: TNS:protocol adapter error in window 10 while coonecting to sql plus

I keep on getting this error . I have corrected ora.listner file as mentioned in other answer of similar problem in stack overflow and I was able to login too but problem start again when I open my window 10 again today.error
listner.ora
Result of lsnrctl status
open listner.ora file without editing and saved it and restart i am able to connect now. Every time i have to do this to connect
Most likely the Oracle service hasn't started. Open up powershell and check it with
get-service OracleService*
If it says it's stopped then you can do
get-service OracleService* | start-service
If it has started then your oracle_sid environment variable hasn't been set or your oracle_home isn't correct.
Unfortnately, I cannot see where you showed the actual sqlplus command, just a screen shot that picks up after sqlplus gets control. But I find the near 100% of the time I see this error, it is because sqlplus is started without specifying a connect descriptor. Doing so means you are asking for a local connection, so the listener, nor any other TNS component is not involved at all. Instead, sqlplus tries to make a local (in memory) connection to the running instance that is specified by the environment variable ORACLE_SID:
TNS Connection:
G:\scripts>sqlplus scott/tiger#edstest
SQL*Plus: Release 12.1.0.2.0 Production on Tue Oct 27 15:46:01 2020
Copyright (c) 1982, 2019, Oracle. All rights reserved.
Last Successful login time: Tue Oct 27 2020 15:45:42 -05:00
Connected to:
Oracle Database 12c Standard Edition Release 12.1.0.2.0 - 64bit Production
SQL>
local connection, check ORACLE_SID first
G:\scripts>echo %ORACLE_SID%
edstest
G:\scripts>sqlplus scott/tiger
SQL*Plus: Release 12.1.0.2.0 Production on Tue Oct 27 15:47:32 2020
Copyright (c) 1982, 2019, Oracle. All rights reserved.
Last Successful login time: Tue Oct 27 2020 15:46:01 -05:00
Connected to:
Oracle Database 12c Standard Edition Release 12.1.0.2.0 - 64bit Production
SQL>
Local connection with ORACLE_SID not set, or improperly set:
G:\scripts>set ORACLE_SID=FUBAR
G:\scripts>echo %ORACLE_SID%
FUBAR
G:\scripts>sqlplus scott/tiger
SQL*Plus: Release 12.1.0.2.0 Production on Tue Oct 27 15:48:57 2020
Copyright (c) 1982, 2019, Oracle. All rights reserved.
ERROR:
ORA-12560: TNS:protocol adapter error
Enter user-name: ^C
G:\scripts>set ORACLE_SID=
G:\scripts>echo %ORACLE_SID%
%ORACLE_SID%
G:\scripts>sqlplus scott/tiger
SQL*Plus: Release 12.1.0.2.0 Production on Tue Oct 27 15:49:59 2020
Copyright (c) 1982, 2019, Oracle. All rights reserved.
ERROR:
ORA-12560: TNS:protocol adapter error
Enter user-name:
And there's your error.

Execute sql statement as sys as sysdba

I have a sql statement executed in a script which connects to sqlplus and execute some GRANTS statements. In the bash script the instruction is something like:
sqlplus sys as sysdba #script.sql
but I need to add the password. How can I do in a single line ?
I tried:
sqlplus "sys as sysdba"/password #script.sql
or without " but it does not work.
Thanks
Example 1
oracle#esmd:~> sqlplus / as sysdba #ulcase1.sql
SQL*Plus: Release 11.2.0.3.0 Production on Fri Feb 21 12:07:50 2020
Copyright (c) 1982, 2011, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Release 11.2.0.3.0 - 64bit Production
Disconnected from Oracle Database 11g Release 11.2.0.3.0 - 64bit Production
oracle#esmd:~>
Example 2
oracle#esmd:~> sqlplus sys/password as sysdba #ulcase1.sql
SQL*Plus: Release 11.2.0.3.0 Production on Fri Feb 21 12:08:44 2020
Copyright (c) 1982, 2011, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Release 11.2.0.3.0 - 64bit Production
Disconnected from Oracle Database 11g Release 11.2.0.3.0 - 64bit Production
oracle#esmd:~>
Example 3
oracle#esmd:~> sqlplus sys/password#esmd as sysdba #ulcase1.sql
SQL*Plus: Release 11.2.0.3.0 Production on Fri Feb 21 12:14:49 2020
Copyright (c) 1982, 2011, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Release 11.2.0.3.0 - 64bit Production
Disconnected from Oracle Database 11g Release 11.2.0.3.0 - 64bit Production
oracle#esmd:~>
You can execute it on this way:
sqlplus sys/password as sysdba #script.sql

oracle instantclient 12c EZCONNECT in Powershell Prompt for password

I have installed instantclient 12c x86 on Windows 8.1
I am able to connect from cmd.exe:
C:\Users\vagrant>sqlplus user#\"hostname:1521/SERVICE_NAME\"
SQL*Plus: Release 12.1.0.2.0 Production on Thu Dec 3 06:22:31 2015
Copyright (c) 1982, 2014, Oracle. All rights reserved.
Enter password:
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, Real Application Clusters, Automatic Storage Management,
OLAP,
Data Mining and Real Application Testing options
SQL>
Now when I try the same from Powershell:
I want to be prompted for password. It does not work.
PS C:\Users\vagrant> sqlplus user#\"hostname:1521/SERVICE_NAME\"
SQL*Plus: Release 12.1.0.2.0 Production on Thu Dec 3 06:30:12 2015
Copyright (c) 1982, 2014, Oracle. All rights reserved.
ERROR:
ORA-12154: TNS:could not resolve the connect identifier specified
Enter user-name:
Here I provide my password and it works
PS C:\Users\vagrant>sqlplus user/password#hostname:1521/SERVICE_NAME
SQL*Plus: Release 12.1.0.2.0 Production on Thu Dec 3 06:24:48 2015
Copyright (c) 1982, 2014, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
Data Mining and Real Application Testing options
SQL>
What is the correct syntax for being prompted for password logging in with sqlplus from Powershell?

Resources