I want to clear the table from all data (Table_with_DB (it is in program sqldeveloper)) through sqlplus and based on filenames in the path (path/to/files) using SQLPlus completes the table (Table_with_DB) the filenames that are in the path.
I create 2 separate SQL files
(clear.sql ; register.sql)
and 1 with bash (bashloop.sh)
clear.sql
BEGIN
package.clear('Table_with_DB');
END;
register.sql
BEGIN
package.register('folderName' , '&1);
END;
bashloop.sh
for i in path/to/files;
do
sqlplus -s userid/password# #clear.sql
sqlplus -s userid/password# #register.sql
done
I expect the query clear Table_with_DB and transfer the filenames from path/to/files to Table_with_DB using SQLPlus
but the actual it not work :(
Example sqlplus in loop .
#!/bin/bash
username=system
password=passwordsystem
tns_alias=esmd
for i in /opt/oracle/example1/test*/file*.txt;
do
$ORACLE_HOME/bin/sqlplus $username/$password#$tns_alias <<EOF
SET SERVEROUTPUT ON SIZE 2000
BEGIN
dbms_output.put_line('$i');
END;
/
EOF
done;
Example output
Connected to:
Oracle Database 11g Release 11.2.0.3.0 - 64bit Production
SQL> SQL> 2 3 4 /opt/oracle/example1/test2/file2.txt
PL/SQL procedure successfully completed.
SQL> Disconnected from Oracle Database 11g Release 11.2.0.3.0 - 64bit Production
SQL*Plus: Release 11.2.0.3.0 Production on Tue Oct 22 13:59:24 2019
Copyright (c) 1982, 2011, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Release 11.2.0.3.0 - 64bit Production
SQL> SQL> 2 3 4 /opt/oracle/example1/test3/file3.txt
PL/SQL procedure successfully completed.
SQL> Disconnected from Oracle Database 11g Release 11.2.0.3.0 - 64bit Production
SQL*Plus: Release 11.2.0.3.0 Production on Tue Oct 22 13:59:24 2019
Copyright (c) 1982, 2011, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Release 11.2.0.3.0 - 64bit Production
SQL> SQL> 2 3 4 /opt/oracle/example1/test4/file4.txt
PL/SQL procedure successfully completed.
SQL> Disconnected from Oracle Database 11g Release 11.2.0.3.0 - 64bit Production
oracle#esmd:~/example1>
If I understand what you want to do then in your bashloop.sh you want to replace db_name with $i - or ${i} if you have filenames with spaces.
Related
So I created a procedure in plsql but I want to automatically execute it in shellscript. Like if I insert a table using csv file then the procedure will automatically run using unix shellscript. The software that I'm using is cygwin.
I'm on MS Windows and don't know Unix nor have I ever used Cygwin, which - as tag description says - is ...
... a collection of GNU and other Unix-like FOSS tools which run on MS Window
If I understand it correctly, you'd then
create a stored procedure (which does some job; mine just displays a message),
SQL (.sql) script which calls the stored procedure, and
MS DOS batch (.bat) script whose task is to establish connection to the database and call the previously mentioned .sql script
This is a procedure:
SQL> create or replace procedure p_test is
2 begin
3 dbms_output.put_line('Hello!');
4 end;
5 /
Procedure created.
SQL>
.SQL script (its name is run_proc.sql):
set serveroutput on
begin
p_test;
end;
/
exit;
MS DOS batch (.bat) script; its name is run_proc.bat:
sqlplus scott/tiger#pdb1 #run_proc.sql
Testing is simple - at MS DOS prompt, call the batch script:
c:\temp>run_proc
c:\temp>sqlplus scott/tiger#pdb1 #run_proc.sql
SQL*Plus: Release 21.0.0.0.0 - Production on Sun Sep 18 07:51:59 2022
Version 21.3.0.0.0
Copyright (c) 1982, 2021, Oracle. All rights reserved.
Last Successful login time: Sat Sep 17 2022 17:43:08 +02:00
Connected to:
Oracle Database 21c Express Edition Release 21.0.0.0.0 - Production
Version 21.3.0.0.0
Hello!
PL/SQL procedure successfully completed.
Disconnected from Oracle Database 21c Express Edition Release 21.0.0.0.0 - Production
Version 21.3.0.0.0
c:\temp>
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.
Is there is any command to find my installed oracle version from windows command prompt. (just like java -version..)
You need to locate ORACLE_HOME directory, to set it as environment variable and then to run opatch:
set ORACLE_HOME=...
"ORACLE_HOME"\OPatch\opatch lspatches
This command lists the installed patches with the Oracle major version.
You can use the below to get the database version by navigating to the ORACLE_HOME directory
$ORACLE_HOME\OPatch\opatch lsinventory | find "Oracle Database"
Well, that depends on what you really have.
If you have installed Oracle onto your PC (such as my 11gXE), then you could try sqlplus -v:
c:\Temp>sqlplus -v
SQL*Plus: Release 11.2.0.2.0 Production
11.2.0.2.0; is that really so?
c:\Temp>sqlplus scott/tiger
SQL*Plus: Release 11.2.0.2.0 Production on Sri Svi 6 07:40:29 2020
Copyright (c) 1982, 2014, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
SQL>
Yes, it is. I use SQL*Plus 11.2.0.2.0 which connects me to 11.2.0.2.0 database.
However, if I don't have Oracle installed on my PC but it is somewhere on database server and I access it via network, then it won't work because it'll display SQL*Plus (i.e. client installed on my PC), not database version. Besides, I can have several clients installed, so it depends on which sqlplus executable I ran.
Database version - in such a case - can't be retrieved from operating system command prompt only (at least, I don't know how to do it). You can obtain it by connecting to the database itself, e.g.
Client version:
M:\>sqlplus -v
SQL*Plus: Release 11.2.0.1.0 Production
ORCL database version is 11.2.0.4.0
M:\>sqlplus scott/tiger#orcl
SQL*Plus: Release 11.2.0.1.0 Production on Sri Svi 6 08:12:27 2020
Copyright (c) 1982, 2010, 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
ORCL2 database version is 10.2.0.5.0:
M:\>sqlplus scott/tiger#orcl2
SQL*Plus: Release 11.2.0.1.0 Production on Sri Svi 6 08:13:42 2020
Copyright (c) 1982, 2010, Oracle. All rights reserved.
Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.5.0 - 64bit Production
With the Partitioning, Real Application Clusters, Data Mining and Real Application Testing options
SQL>
When you're in SQL*Plus, query v$version, e.g.
SQL> select * From v$version where rownum = 1;
BANNER CON_ID
-------------------------------------------------------------------------------- ----------
Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production 0
SQL>
which shows that I'm currently connected to 12.2.0.1.0.
This code allows you to display only the version of the database.
#echo off
set user_name=scott
set password=tiger
set net_service_name=edcu
set sql=select version from PRODUCT_COMPONENT_VERSION where rownum=1;
(echo conn %user_name%/%password%#%net_service_name%
echo set heading off
echo %sql%
echo exit) |sqlplus -s /nolog
For example output
D:\upwork\stackoverflow\get_version_oracle>get_oracle_version.bat
11.2.0.3.0
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
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!!