I want populate variable from query in oracle package.
In package I'm define:
g_my_value number;
g_my_value2 number;
PROCEDURE initialize_par;
In package body:
create or replace PACKAGE BODY my_pkg AS
PROCEDURE initialize_par AS
BEGIN
Select parameter_value into g_my_value from tbl_parameters where name = 'my_value';
Select parameter_value into g_my_value2 from tbl_parameters where name = 'my_value2';
END initialize_par;
Before use variable in other procedure in this package I need call
Exec initialize_par to set value in variable.
Is it good option? Is it possible set value in variable without procedure initialize_par?
You can initialise a package (without a procedure) using a BEGIN/END initialiser block in the procedure body:
CREATE PACKAGE package_name IS
g_my_value number;
g_my_value2 number;
END;
/
CREATE PACKAGE BODY package_name
IS
BEGIN
Select parameter_value
into g_my_value
from tbl_parameters
where name = 'my_value';
Select parameter_value
into g_my_value2
from tbl_parameters
where name = 'my_value2';
END;
/
Given the sample parameters:
CREATE TABLE tbl_parameters (name, parameter_value ) AS
SELECT 'my_value', 42 FROM DUAL UNION ALL
SELECT 'my_value2', 3.141 FROM DUAL;
then:
BEGIN
DBMS_OUTPUT.PUT_LINE(package_name.g_my_value);
DBMS_OUTPUT.PUT_LINE(package_name.g_my_value2);
END;
/
Outputs:
42
3.141
fiddle
Related
declare
TYPE stag_tab IS TABLE OF d_staging%ROWTYPE;
stag_tab1 stag_tab;
begin
--Bulk Collect
select * bulk collect into staging_tab1 from d_staging;
PKG_T.PROC_T(stag_tab1);
end;
/
Package definition:
--Package
CREATE OR REPLACE PACKAGE PKG_T
AS
TYPE staging IS TABLE OF d_staging%ROWTYPE;
PROCEDURE PROC_T(p_staging IN staging);
END PKG_T;
/
-- Package Body
CREATE OR REPLACE PACKAGE BODY PKG_T
AS
PROCEDURE PROC_T (p_staging IN staging)
AS
VAR1 d_staging%ROWTYPE;
CUR1 SYS_REFCURSOR;
QUERY_STRING VARCHAR2(2000);
BEGIN
OPEN CUR1 FOR SELECT * from table(p_staging);
LOOP
FETCH CUR1 into VAR1;
EXIT WHEN cur1%NOTFOUND;
INSERT into d (testdata) VALUES (var1.testval1);
COMMIT;
END LOOP;
END;
END PKG_T;
/
You are receiving the error because the procedure PKG_T.PROC_T is expecting a parameter of type staging, but when you are calling the procedure you are passing it a variable of type stag_tab. The type of the variable being passed to the procedure needs to match the type of the parameter definition for the procedure.
Your procedure declaration:
PROCEDURE PROC_T (p_staging IN staging)
Takes the argument as type staging.
You are passing the argument as a locally defined type:
TYPE stag_tab IS TABLE OF d_staging%ROWTYPE;
These are different types. Instead, you need the PL/SQL block to be:
declare
stag_tab1 package_name.staging;
begin
select *
bulk collect into stag_tab1
from d_staging;
PKG_T.PROC_T(stag_tab1);
end;
/
I am new to plsql i try to run the piece of code but it is giving error and not able to debug
create or replace type final as object ( ename1 varchar2(10), sal1
NUMBER(7,2));--object
create or replace type construct is table of final; /*nested table of
object type */
create or replace function returnmore (empno1 number) /*Function to
return more*/
return construct
AS
vemp construct:=construct();
vename varchar2(10);
vsal1 NUMBER(7,2);
begin
select ENAME,sal into vename,vsal1 from emp where empno=empno1;
vemp.extend;
vemp(1):=construct(vename,vsal1);
return vemp;
end;
But gives me an error
Function SYSTEM.RETURNMORE#loacaDB
Error(11,1): PL/SQL: Statement ignored
Error(11,10): PLS-00306: wrong number or types of arguments in call to 'CONSTRUCT'
I am using oracle10gxe and sqldeveloper4.2
You can prefer using non-preserved keyword such as typ_emp instead of final which's reserved.
SQL> create or replace type typ_emp as object ( ename1 varchar2(10), sal1 number(7,2));
SQL> create or replace type construct is table of typ_emp;
and you can convert your function as below :
create or replace function returnmore( empno1 emp.empno%type )
return construct AS
vemp construct := construct();
vename varchar2(10);
vsal1 number(7, 2);
begin
select ename, sal into vename, vsal1 from emp where empno = empno1;
vemp.extend;
vemp(1) := typ_emp(vename, vsal1);
dbms_output.put_line(vemp(1).ename1);
return vemp;
end;
/
or another way to handle the same operation :
SQL> create or replace function returnmore( empno1 emp.empno%type )
return construct AS
vemp construct := construct();
v_sql varchar2(2000);
begin
v_sql := 'select typ_emp(ename, sal) from emp where empno = :v_empno1';
execute immediate v_sql bulk collect into vemp using empno1;
dbms_output.put_line(vemp(1).ename1);
return vemp;
end;
/
and test by invoking
SQL> set serveroutput on;
SQL> declare
result construct;
begin
result := returnmore( 1 ); -- 1 is just an ordinary presumed value for empno
end;
/ --> this will return the employee name as printed.
P.S. Never ever use SYSTEM user for non-administrative purposes. May be extremely harmful for your database.
i'm kind of new to Oracle Pl\SQL. I was just trying to create a simple Package with a procedure that returns a set of object id's; the code is as follows:
--Package Spec
CREATE OR REPLACE PACKAGE TEST IS
--GET OBJECT ID'S FROM CONTROL TABLE
PROCEDURE get_object_id_control(p_obj_id OUT abc_table%ROWTYPE);
END;
--Package Body
PROCEDURE get_object_id_control(p_obj_id OUT abc_table%ROWTYPE) AS
BEGIN
SELECT object_id
INTO p_obj_id
FROM abc_table
WHERE fec_proc IS NULL;
END;
I get Error: PL/SQL: ORA-00913: too many values. Is this the correct way for returning multiple values of same data type, or is there a better approach. Thanks in advance.
You can create a custom table type and set the out parameter of the procedure to that type.
CREATE TABLE ABC_TABLE(ID varchar2(100));
create or replace type abc_tab is table of varchar2(100);
/
CREATE OR REPLACE PACKAGE TEST IS
PROCEDURE get_object_id_control(p_obj_id OUT abc_tab);
END;
/
CREATE OR REPLACE PACKAGE BODY TEST IS
PROCEDURE get_object_id_control(p_obj_id OUT abc_tab) AS
BEGIN
SELECT id
bulk collect INTO p_obj_id
FROM abc_table;
END;
END;
/
Then you can call it like so:
declare
v abc_tab;
begin
TEST.get_object_id_control(p_obj_id => v);
for i in v.first..v.last loop
dbms_output.put_line(v(i));
end loop;
end;
/
Similar to GurV's answer (since he beat me by like 30 seconds...), you can use a PL/SQL object type as well. You do not need the CREATE TYPE statement if you don't need to reference the type in SQL.
--Package Spec
CREATE OR REPLACE PACKAGE TEST AS
TYPE id_table_type IS TABLE OF NUMBER;
--GET OBJECT ID'S FROM CONTROL TABLE
PROCEDURE get_object_id_control(p_obj_id_list OUT id_table_type);
END;
--Package Body
CREATE OR REPLACE PACKAGE BODY TEST AS
PROCEDURE get_object_id_control(p_obj_id_list OUT id_table_type) AS
BEGIN
SELECT object_id
BULK COLLECT INTO p_obj_id_list
FROM abc_table
WHERE fec_proc IS NULL;
END;
END;
To use it:
DECLARE
l_id_list test.id_table_type;
BEGIN
test.get_object_id_control (p_obj_id_list => l_id_list);
FOR i IN l_id_list.FIRST .. l_id_list.LAST LOOP
DBMS_OUTPUT.put_line (l_id_list (i));
END LOOP;
END;
I créated an Oracle stored procedure into a package. I did like this:
CREATE OR REPLACE PACKAGE PACKFACE IS
TYPE LIST_IDS IS TABLE OF INT INDEX BY BINARY_INTEGER;
PROCEDURE P_SELECT_IDBFRIENDS (CONSULTA OUT SYS_REFCURSOR,COD_US IN INT,IDS_NOT IN LIST_IDS);
END;
And the body of the package is:
CREATE OR REPLACE PACKAGE BODY PACKFACE IS
PROCEDURE P_SELECT_IDBFRIENDS (CONSULTA OUT SYS_REFCURSOR,COD_US IN INT, IDS_NOT IN LIST_IDS) IS
BEGIN
OPEN CONSULTA FOR
SELECT ID_US1,ID_US2 FROM T_FRIENDSHIP WHERE ID_US1=COD_US AND ID_US2 NOT IN (SELECT COLUMN_VALUE FROM TABLE(IDS_NOT));
END;
END;
/
These is ok in my Oracle 12c server, but I did the same code in Oracle 11g appeared an error, cannot access rows from a non-nested table ítem. What would be the solution? Thanks in advance
After this problem was fixed. Appears other one my Python code was broken. I have this procedure:
def select_ids(self,cod_us,ids_not):
lista = []
try:
cursor = self.__cursor.var(cx_Oracle.CURSOR)
varray = self.__cursor.arrayvar(cx_Oracle.NUMBER,ids_not)
l_query = self.__cursor.callproc("PROC_SELECT_IDS_ENT_AMISTADES", [cursor, cod_us, varray])
lista = l_query[0]
return lista
except cx_Oracle.DatabaseError as ex:
error, = ex.args
print(error.message)
return lista
PLS-00306 wrong number or type of arguments in call to a procedure. It was ok using Oracle 12c. Thanks in advance again.
You cannot use a collection type defined in PL/SQL in an SQL query in Oracle 11.
If you want to use a collection in both SQL and PL/SQL then you will have to define it in SQL:
CREATE TYPE LIST_IDS IS TABLE OF INT;
Then you can do:
CREATE OR REPLACE PACKAGE PACKFACE IS
PROCEDURE P_SELECT_IDBFRIENDS (
CONSULTA OUT SYS_REFCURSOR,
COD_US IN INT,
IDS_NOT IN LIST_IDS
);
END;
/
SHOW ERRORS;
CREATE OR REPLACE PACKAGE BODY PACKFACE IS
PROCEDURE P_SELECT_IDBFRIENDS (
CONSULTA OUT SYS_REFCURSOR,
COD_US IN INT,
IDS_NOT IN LIST_IDS
)
IS
BEGIN
OPEN CONSULTA FOR
SELECT ID_US1,ID_US2
FROM T_FRIENDSHIP
WHERE ID_US1=COD_US
AND ID_US2 NOT MEMBER OF IDS_NOT;
END;
END;
/
SHOW ERRORS;
First of all you should to create an oracle type like this:
CREATE OR REPLACE TYPE LIST_IDS AS TABLE OF INT;
And after that you should to create the package with the procedure and a nested table in parameter like this:
CREATE OR REPLACE PACKAGE PACKFACE IS
TYPE LISTADO_IDS IS TABLE OF INT INDEX BY PLS_INTEGER;
PROCEDURE P_SELECT_IDBFRIENDS (CONSULTA OUT SYS_REFCURSOR,COD_US IN INT,IDS_NOT IN LISTADO_IDS);
END;
Finally you should create the body. You pass the data to nested table from oracle type like this:
CREATE OR REPLACE PACKAGE BODY PACKFACE IS
PROCEDURE P_SELECT_IDBFRIENDS (CONSULTA OUT SYS_REFCURSOR,COD_US IN INT, IDS_NOT IN LISTADO_IDS)
IS
num_array FACEBOOK.LIST_IDS;
BEGIN
num_array:=LIST_IDS();
for i in 1 .. IDS_NOT.count
loop
num_array.extend(1);
num_array(i) := IDS_NOT(i);
end loop;
OPEN CONSULTA FOR
SELECT ID_US1,ID_US2 FROM T_FRIENDSHIP WHERE ID_US1=COD_US AND ID_US2 NOT IN (SELECT COLUMN_VALUE FROM TABLE(num_array));
END;
END;
I did that and I got the solution to the python error wrong number or type parameters. Good luck.
Oracle 11g Express Edition 11.2.0
Table "RIGHT" data (contains only 1 row with name = Test):
Id | Name
1 Test
2 New2
3 New14
...
SELECT COUNT(*) FROM RIGHT WHERE name = 'Test';
Result = 1
I have procedure in package TEST:
create or replace
PACKAGE BODY TEST
AS
PROCEDURE FIND_RIGHT(rightName IN VARCHAR2)
IS
countrows NUMBER;
BEGIN
SELECT COUNT(*) INTO countrows FROM RIGHT WHERE name = rightName;
/* DEBUG POINT !!! in this point countrows = 212 !!!! */
...
END;
END TEST;
And run it from other procedure:
DECLARE
rightName VARCHAR2(200);
BEGIN
rightName := 'Test';
TEST.FIND_RIGHT(rightName);
END;
I run debugger (in Oracle SQL Developer) and debug point after select.
I see countrows = 212.
Why countrows != 1 ???
UPDATED:
All transaction is commited. Open only 1 session (from SQL Developer). Table Rights has 3 indexes (table is big, I don't write all colums in the post). Procedures have many input params (custom objects), but I drop extra information.
UPDATED #2:
I change code to
create or replace
PACKAGE BODY TEST
AS
PROCEDURE FIND_RIGHT(rightName IN VARCHAR2)
IS
countrows NUMBER;
testVar VARCHAR2(200);
BEGIN
testVar := 'Test';
SELECT COUNT(*) INTO countrows FROM RIGHT WHERE name = testVar;
/* in this point countrows = 1 */
...
END;
END TEST;
I guess your table contains a column called "rightName"?
if this is the case your orginal query would compare the "name" and "rightName" columns instead of using the procedure argument.
Try changing the argument name.