Executing an sql script vi an OCI8 connection object - ruby

I have a locally running oracle thin client and have successfully created a ruby script to connect to a remote oracle database. I successfully make a call (select * from table_name) to the database to get the content of a table:
begin
con = OCI8.new('<user>', <password>, '<host>:<port>/XE')
con.exec('select name from actor') do |records|
puts records
end
rescue OCIError
puts "Database Connection Error"
end
I also want to run an sql script that resides in the oracle directory on the remote host.
Usually I perform the following:
su - oracle
sqlplus <user>/<password>
<SQL> #<script_name>
and this will run the script
In the ruby script I try the following:
con.exec('#<script_name>')
Yet, I get the following error:
stmt.c:230:in oci8lib_200.bundle: ORA-00900: invalid SQL statement (OCIError)

#<script_name> is an sqlplus command.
When sqlplus find #<script_name>, it opens <script_name>, divides its contents to SQL statements and executes them.
If you want to run SQL statements in a script by ruby, you need to write code which opens the script, divides its contents and pass SQL statements to con.exec one by one.
I also want to run an sql script that resides in the oracle directory on the remote host. Usually I perform the following:
No, it can't. sqlplus reads the sql script that resides on the local host.

You can put this script in a function (function_name) and execute
con.exec("select function_name from dual")

Related

Trying to execute sql script using sqlplus from powershell commandline

I'm trying to execute a sql script from powershell using the following command:
sqlplus username/password#tnsnamesalias 'path to my sql file.sql'
If I run the command without the script path, I can connect to the database and execute commands. If I include the script path (which includes spaces) then I just get the sqlplus help text and no changes are made to the database. My sql script is finished with END; and /
What am I doing wrong?
I believe you need to add the # sign before the path:
sqlplus username/password#tnsnamesalias #'path to my sql file.sql'
You have forgotten "#" symbol and the apostrophes are wrong here.
This works for me for executing a "test script.sql" file
sqlplus .... "#test script.sql"

Calling an Oracle stored procedure in Informatica Cloud postprocessing command

I have an Informatica data synchronization task that creates a table in Oracle. I am trying to include a call to an Oracle stored procedure in the postprocessing command of Informatica Cloud that will update a variety of tables at the completion of the task. The procedure that I am trying to call is in the same schema as the target of the synchronization task. The procedure runs correctly when I run it directly in Oracle SQL Developer, but I can't get it to run via Informatica Cloud. I know I'm not using the right syntax to make the call, but these are some examples that I have tried so far:
BEGIN
(PROCEDURE_NAME)
END;
CALL(PROCEDURE_NAME);
EXEC(PROCEDURE_NAME);
PROCEDURE_NAME;
Would designing a mapping in Informatica Cloud help me with this? Or is there a prefix that I should be appending to the stored procedure call, even though the procedure is in the same schema as the target of the task?
None of these options will work in Synchronisation task. Call (procedure_name); will work in Mappings which is the easiest way to do it
In Synchronisation task you need to create a batch file to run a SQL script. The script has to contain the connection details.
Example:
connect user/password#
exec (procedure_name);
disconnect;
exit SQL.SQLCODE
SQL.SQLCODE is used so that it will commit all the transactions.
After that, it will be necessary to use a .bat to run the above sql script using sqlplus client.
Example:
cd \bin
call cmd.exe /C "sqlplus /nolog < {path to sql script created in step
2}\filename.sql"
exit /b 0;

Does SQLPlus exit after running a script?

We are using CA Workload Control Center (Autosys) to run a windows batch file which calls another batch file which calls SQLPlus with the following:
sqlplus username/password %1 %2 %3
Since this is all being done remotely I cannot see what is actually happening on the server.
I am wondering if the sqlplus file exists but has invalid syntax (like referring to non-existent table) will the sqlplus prompt exit and return back to the batch file?
The reason I ask is because I am looking at the log file from Autosys and it shows that SQLPlus call was made, it prints out ERROR AT LINE 2: Invalid Table, but then it does not show any other activity with the batch script after that where there are multiple echoes and file copies etc. It seems as though it is not exiting SQLPlus perhaps?
Is there a parm I need to pass to SQLPlus to tell it to exit SQLPlus and return back to the calling script after running a SQL script if it fails?
Edit: We are using "WHENEVER SQL ERROR" inside of our SQL files as well and the log file does show this:
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
Data Mining and Real Application Testing options
But I am still expecting that it should continue with the rest of the Batch script but its not, above is the last that Autosys shows in the log
See SQLPlus instruction WHENEVER SQLERROR, it describes in Oracle docs:
http://docs.oracle.com/cd/B19306_01/server.102/b14357/ch12052.htm
Found the solution SO Post:
You can do sqlplus -l test/Test#mydatabase #script.sql; the -l flag means it will only try to connect once, and if it fails for any reason will exit instead of prompting. Look at the output of sqlplus -?, or see the documentation.

using strace to debug db2 CLI executing a query

I would like to use strace to debug a strange behaviour I have with db2. I have a SQL function myFoo() implemented in C that doesn't get called for some reason (some code access path not existing or not authorized see here). My Sql Function call a function Fooin shared library in /usr/local/lib/libmyfoo (so in db2 term /usr/local/lib/libmyfoo!Foo).
If I use strace directly with db2 and the query I have an error saying
A database connection does not exist
so i created a script call debug.sh with the following. The idea is to have a shell with the db2 connection active and trace it.
db2 "connect to MYDB"
db2 "select * from table(myFoo())" # this calls /usr/local/lib/libmyfoo!Foo
db2 "disconnect MYDB"
It doesn't work cos I realized that strace works with binary so I have the error
Exec Format Error
Probably you are calling each DB2 command in different subshells.
You can fix that problem by executing everything in just one subshell, for example
VALUE=$(. /home/db2inst1/sqllib/db2profile ; db2 connect to MYDB ; db2 "select * from table(myFoo())")
My Sql Function call a function Foo in shared library in /usr/local/lib/libmyfoo (so in db2 term libmyfoo!Foo).
Is this really how you defined your function? libmyfoo!Foo will point to the library $INSTANCE_HOME/sqllib/function/libmyfoo. If your library is elsewhere you need to provide the absolute path to it.

Execute SQL query on Oracle working in Unix prompt but not in shell script

I am trying to connect to an Oracle db using a ksh script. When I run this directly from the prompt, it works. But when I put it inside a script (abc.sh) it fails. Below is what I've put in the script (edited to make it shorter):
Here abc is the username, while abc$123 is the password of the user which has access on database DBNAME.
#!/usr/bin/ksh
sqlplus -s /nolog << EOF > output
connect abc/abc$123#DBNAME;
set echo off
set heading off
select table_name from dba_tables;
exit;
EOF
This works if typed directly, but run as ./abc.sh, gives error -
ERROR ORA-01017: invalid username/password; logon denied
I'm sure I'm missing something simple, but can't figure this out. Thanks for your help.
You cannot just write code like this and expect it to be passed to the relevant program. Your SQL lines are being executed by KSH instead of your SQL server. Instead you need to do something more along the lines of:
echo "SQL CODE HERE" | sqlplus ....

Resources