Oracle PL/SQL dbms_output to Jenkins console - oracle

Using Cloudbees SDA CI to implement Jenkins, with Oracle 19 P/SQL running in many jobs. Using dbms_output in the Oracle script, the output does not show up in the Jenkins console at all. I'm looking for functionality that would allow me to print to the Jenkins console from P/SQL the same way I can from say Python using the print statement. I'm generally trying to do this currently:
set serveroutput on;
begin
[...a bunch of code...]
dbms_output.put_line('This is a dbms_output call...');
end;
/
Thank you!

Related

PL/SQL from Oracle SQL Developer connecting to DB2

I have successfully connected to a DB2 database from Oracle SQL Developer 19.4.0.354 using the DB2 ODBC driver.
SQl scripts work just fine.
Now I also want to execute PL/SQL scripts but simple sample script fails.:
set serveroutput on
DECLARE
message varchar2(20):= 'Hello, World!';
BEGIN
dbms_output.put_line(message);
END;
It fails in such a way that PL/SQL code does not seem to be accepted at all:
DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=DECLARE
message varchar2(20):= 'Hell;BEGIN-OF-STATEMENT;<create_view>, DRIVER=4.26.14
Is PL/SQL generally supported in this setup?
When using the Oracle-compatibility mode of Db2-LUW, it is necessary to:
set the Db2-registry-variable DB2_COMPATIBILITY=ORA with the db2set command.
bounce the Db2-instance with the db2stop and db2start commands, so the registry variable becomes effective.
create a new database after both of the above steps are completed (using the db2 create database command ), so that Oracle specific datatypes and other features become available.
If you only change the registry variable and bounce the Db2-instance but use a pre-existing database then you will not get all of the implemented Oracle compatibility features! This is documented in the Db2-Knowledge-Centre online.
With currently shipping Db2-LUW versions, you cannot retrofit the Oracle compatibility on to a previously created database.

Issue with "spool"-commands in PL SQL Procedure

I was trying to create a procedure within one of the Oracle DBs. Procedure is intended for my colleagues, so that everybody could export the result of the query in a csv-file.
I was using "spool" commands. When running the code between BEGIN and END separately in Oracle SQL Developer (running as script, F5) - it worked, and created the file locally.
Then I tried to put it in the procedure, and that is where issue began. Some compilation errors come out. Most of them have code "PLS-00103". I tried searching for it, but not successful so far. I even tried EXECUTE IMMEDIATE statement, putting some script blocks inside it - not working so far.
I will appreciate any hints/help on approaching my situation.
P.s. one of my first attempts is below:
CREATE OR REPLACE PROCEDURE export_to_csv (SOURCE IN VARCHAR2, EXPORT_PATH IN VARCHAR2) is
BEGIN
spool on;
set feedback off;
set heading off; ​ ​ ​
set sqlformat csv;
spool EXPORT_PATH;
select * from SOURCE;
spool off;
END export_to_csv;
Nope, that won't work. SPOOL, as well as all SET commands you use, are SQL*Plus.
Stored procedure is PL/SQL, so - if you want to do it from there, you'll have to use UTL_FILE package. The result will be on the database server, not your local PC.
Alternatively:
put those SPOOL, SET, SELECT, ... commands into a .SQL file on the database server
create a database job using DBMS_SCHEDULER package which is capable of running operating system files. On MS Windows, you'd call CMD which establishes connection to the database using SQLPLUS executable and calls your .SQL script (using #)
just like in the previous option, the result will be on the database server, not on a local PC
See which option you prefer.
Maybe it would be simpler to create that .SQL file and give it to all your colleagues who'd then run it on their PCs and get the result locally.

Continue running SQL procedure when SQL Developer is Closed

Is there any way to execute a procedure in SQL Developer and have the procedure continue to run even in SQL Developer is closed?
Yes, here's an Oracle doc that will guide you through defining a scheduler job. You can use this method (or DBMS_SCHEDULER, or OEM) to schedule a job that will execute a procedure without needing to maintain database connectivity.
Would recommend reading the entire doc, but you can skip ahead to "Schedule SQL Scripts Using SQL Developer" to view the relevant info.
https://docs.oracle.com/database/sql-developer-17.2/DMRIG/generating-deploying-sql-scripts.htm#DMRIG260

How to create directory from a stored procedure on an Oracle server machine? [duplicate]

I am having a problem using host() command in oracle procedure.
I have written very simple oracle code.
CREATE OR REPLACE PROCEDURE
run_command(command_i IN VARCHAR2)
IS
l_message VARCHAR2 (100);
BEGIN
l_message := 'cmd ' || command_i;
host(l_message);
END run_command;
when host(l_message); is eliminated works fine.
Whats the problem and is there anyway to create a routine which uses host()?
The HOST command is only available in SQL*Plus and not from PL/SQL.
You can use Java stored procedure to call call OS commands. Oracle released a white paper on calling OS commands from within PL/SQL back in 2008 but there's plenty of other stuff out there (including Oracle Base, which is quite good)
Another clunky, but non-Java way would be to create DBMS_SCHEDULER ad-hoc EXECUTABLE job which is your HOST command (e.g. ls ), and then execute the job.
Note these run on the database server, not on your remote/local client.

Is pl/sql don't use sql*plus command in Oracle?

example:
create or replace procedure DEMO is
begin
--host dir
--execute immediate 'host dir';
end DEMO;
I pupose invoke window's procedure in Oracle.
I try to use #command in sql*plus, but it discontent my requirement.
Stored procedures run on the database server. SQL*Plus commands are executed on the client. You cannot, therefore, use SQL*Plus commands in a stored procedure.
There is no way realistically for a stored procedure to get a directory listing for a directory on the client machine. (You could, I suppose, have the client expose a shared directory and mount that from the server and the read the contents from the database but that isn't something that would be realistic in the vast, vast majority of cases). A stored procedure can potentially call out to the database server's operating system using a Java or CLR stored procedure or, depending on the version, using the dbms_scheduler package. But there are a lot of security issues to be concerned about before implementing something like this.

Resources