call procedure which is inside a package in oracle 10g - oracle10g

how to call procedure which is inside a package in oracle 10g.
i have a two procedure called "area" which is of one parameter and other is also "area" but with 3 parameters. in short procedures are overloaded inside a package "shapearea".
create or replace package shapearea is
procedure area(l in number);
procedure area(l in number,b in number,h in number);
end;
create or replace package body shapearea is
procedure area (l in number) is
begin
dbms_output.put_line('Area of '||'l'||' lengh circle is'||l*l);
end;
procedure area (l in number,b in number,h in number) is
begin
dbms_output.put_line('Area of '||'l'||' lengh ractangle is'||l*b*h);
end;
end;
i tried
execute shapearea.area(5);
exec shapearea.area(5);
call shapearea.area(5);
shapearea.area(5);
but it did not work in oracle 10g.

If you are using SQL*Plus then try:
set serveroutput on
Begin
shapearea.area(5);
end;
/
If you are using SQL Developer you need to show the DBMS_Output window and enable it for the connection you are using.
DBMS_OUTPUT does not actually write to your display on its own. In reality it just buffers the output to some internal data structures, and then in the case of SQL*Plus, the SET SERVEROUTPUT ON enables client side routines to retrieve the output. In the case of SQL Developer showing the DBMS Output window and enabling it for the connection you are using enables it's client side display routines.
For other environments you may need to programatically read out the buffered results with the GET_LINE and/or GET_LINES DBMS_OUTPUT functions.

Related

Stored procedure return value Db2

CREATE PROCEDURE A()
LANGUAGE SQL
RESULT SETS 1
BEGIN
DECLARE C1 CURSOR WITH RETURN FOR
SELECT id, name, dept, job FROM staff;
OPEN C1;
END
CREATE PROCEDURE B()
LANGUAGE SQL
BEGIN
Call a ;
END
In programming, they don't use stored procedure for returning value (they use function). Why does DB2 support that way?
In my above case, how does the stored procedure B know c1 cursor from stored procedure A? If I declare a cursor (c2 cursor) in stored procedure B and send to stored procedure A. Stored procedure A has a parameter (OUT c2 cursor), so why must I write 'RESULT SETS 1' (Can't I omit that phrase), because I don't return any cursor from stored procedure A (I return by parameter).
You might benefit from some education and training about SQL PL.
If you prefer a paper book, get "Db2 SQL Procedural Language for Linux, Unix, and Windows" by Paul Yip, Drew Bradstock and others. ISBN 0-13-100772-6.
Stackoverflow is not a substitute for training and education.
You should ask separate questions for different topics.
Research Db2 'table functions' to learn how to return a table from a function.
Research Db2 'strongly typed cursors', and Db2 'weakly typed cursors' and pipelined functions, to learn how to exploit cursor parameters in routines. Understand the many restrictions and rules associated with these things, which cannot be conveyed by one example in one question. In particular, realise that SQL PL cursor parameters can only be fully manipulated by SQL PL and cannot currently be passed to other languages for processing (except jdbc which has support for consuming such cursors). So if your front end client is using C, C++, Python, Javascript, .Net, PHP etc, then you won't use SQL PL cursor parameters currently. You might use them inside Db2 SQL PL code however, depending on your skills and needs.
To consume a result set from a nested stored procedure, you need extra syntax. When returning a result-set from a stored-procedure the dynamic result sets clause is required when defining the procedure. Returning a result set in this manner is understood by many different programming languages and frameworks, and by other RDBMS tools, so is the most general purpose method for returning data from an RDBMS to a 3rd Generation programming language. Other mechanisms can be built based on this technique.
All of this syntax is documented in the Db2 Knowledge Centre for your version and platform. One example cannot convey all possible syntax.
Here is an example, you will find many others online if you are competent with search.
--#SET TERMINATOR #
CREATE or replace PROCEDURE procA()
LANGUAGE SQL
RESULT SETS 1
BEGIN
DECLARE C1 CURSOR WITH RETURN TO CALLER FOR
SELECT id, name, dept, job FROM staff;
OPEN C1;
END
#
set serveroutput on#
CREATE or replace PROCEDURE B()
LANGUAGE SQL
BEGIN
declare sqlstate char(5) default '00000';
declare v_rs result_set_locator varying;
declare v_id smallint;
declare v_dept smallint;
declare v_name varchar(9);
declare v_job char(5);
Call procA() ;
associate result set locator (v_rs) with procedure procA;
allocate v_rscur cursor for result set v_rs;
fetch from v_rscur into v_id, v_name, v_dept, v_job;
while ( sqlstate = '00000') do
-- do something with the values just fetched...
-- i.e. process the data in the current row of the result-set
call dbms_output.put_line('id:'||varchar(v_id)||' name: '||v_name||' dept: '||varchar(v_dept)||' job: '||v_job);
-- in this example just write the data to the output stream
fetch from v_rscur into v_id, v_name, v_dept, v_job;
end while;
return;
END
#

From SQL Server to Postgres

I'm new to Postgresql; I have only used SQL Server before so I'm trying to migrate an ASP.NET MVC application to ASP.NET Core 3.0 and replace SQL Server with PostgreSQL in the process, and move to Ubuntu.
Can anyone tell what's wrong with my query here?
CREATE OR REPLACE PROCEDURE officekit.testsp (mode CHARACTER)
LANGUAGE plpgsql AS $plpgsql$
BEGIN
IF mode = 'test' THEN
SELECT a,b,c,d FROM testSchema.test;
END IF;
COMMIT;
END;
$plpgsql$;
In your code almost every line is not good.
First if you want to write stored procedures in Postgres, please start with the documentation. The concept of stored procedures in MSSQL is significantly different to that in Oracle, DB2, and certainly different to that in Postgres. PLpgSQL in Postgres is similar to Oracle's PL/SQL. The best thing to do, is to forget all that you know about stored procedures and start from scratch.
The errors:
procedures in Postgres cannot return data - only functions can do this - in your example you probably need a table function.
CREATE OR REPLACE FUNCTION officekit.testsp(mode text)
RETURNS TABLE(a text, b text, c text)
AS $$
BEGIN
IF mode = 'test' THEN
RETURN QUERY SELECT test.a, test.b, test.c FROM test;
END IF;
END;
$$ LANGUAGE plpgsql;
Personally I don't like this style of programming. Functions should not just wrap queries. A lot of significant performance problems and architecture issues arise from the bad use of this possibility, but it depends on context.
Why is commit there? This code doesn't make any change in the database. There is no reason to call commit.

How to do a sub procedure in sql / pl

I am doing a procedure migration in pl / sql (oracle) to sql / pl (DB2) and I don't know how to pass a subprocedure to DB2
As I am not a system administrator I cannot change DB2 to be pl / sql compatible
EXAMPLE
create or replace PROCEDURE "SP_NOSTRADAMUS_PRODUTO"
AS
V_EXISTE_TAB NUMBER := 0;
PROCEDURE PR_HIRQ_PRODUTO_OR
AS
BEGIN
END
If your Db2-server runs on Linux/Unix/Windows, in ANSI SQL PL, if you have a local procedure nested within the body of another SQL PL procedure (or compound statement), then you use the DECLARE keyword to define the local procedure. This is supported by Db2-LUW in versions v10.1 and higher.
If you have very many Oracle PL SQL stored procedures, please discuss with your solution architect (if you have one) and your DBA about the possibility of configuring your Db2 server to support Oracle compatibility . This may save you some money.
You can see the Db2 syntax for SQL PL here (be sure to select the correct version of your Db2-server product in the pull down list).
There are some restrictions on local procedures, so study the documentation carefully. But your local procedure can access variables and objects defined in surrounding block(s) that are in scope.
Your example might look like this in ANSI SQL PL:
create or replace PROCEDURE "SP_NOSTRADAMUS_PRODUTO"
begin
declare V_EXISTE_TAB integer default 0;
declare PROCEDURE PR_HIRQ_PRODUTO_OR
BEGIN
-- body of pr_hirq_produto_or procedure
END;
-- body of sp_nostradamus_produto procedure
END
#
Db2 does not have subprocedures, not even sure why oracle would. In db2 you just create the 'sub' procedures as regular procedures and then 'CALL' them.
create procedure a( ....
create procedure b ....
create procedure main(...)
begin
call a(...);
call b(...);
end

DB2 on cloud not supporting pl/sql

I'm trying to run PL/SQL code like
DECLARE
sum INTEGER := 0;
BEGIN
LOOP
sum := sum + 1;
IF sum > 10 THEN
EXIT;
END IF;
END LOOP;
END
But the code above and, in fact, any PL/SQL code is not getting executed and throws errors.
You can execute PL/SQL statements against Db2 on Cloud when using the proper client and when the Oracle compatibility mode is active. Not every database client supports executing the statements because of their structure, handling of terminators and the way PL/SQL and SQL/PL statements and procedures (in general) are called.

Calling external program from PostgreSQL trigger

I would like to execute external program (such as .net c# console) when PostgreSQL trigger is fired. How can I achieve it?
Postgres cannot normally run external programs for security reasons.
The typical solution is to use NOTIFY and have a daemon LISTEN to it. There are solutions for every major scripting language out there ...
Examples for Java from #Craig: How to refresh JPA entities when backend database changes asynchronously?
Relevant manual page for PHP.
Since Postgres 9.3 there is a solution for invoking external programs. It is - for security reasons - limited to superusers and IMHO intended for exporting data, rather than doing a "notification on trigger":
COPY (SELECT 1) TO PROGRAM '/bin/touch /tmp/created_by_postgres'
If you want to actually export data to the invoked programm, you can provide any SELECT or a table name instead of SELECT 1. The query results will then be passed to the invoked program via its standard input.
You can find documentation of the feature in the Postgres docs:
http://www.postgresql.org/docs/9.3/static/sql-copy.html
You can execute external scripts from inside trigger function with an "untrusted" language, like plpythonu.
More details here: https://www.postgresql.org/docs/current/plpython.html
Trigger function example:
CREATE FUNCTION execute_python_script()
RETURNS trigger
AS $$
begin
import subprocess
result = subprocess.run(['/path/to/your/bin/python', '/some_folder/some_sub_folder/script.py'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
end;
$$
LANGUAGE plpythonu;
Trigger example:
CREATE TRIGGER trigger_name
AFTER INSERT ON table
EXECUTE PROCEDURE execute_python_script();