how to call function from package by creating own collection of same collection type which is being used in package? can we use our own created collection to call function from packages? getting error while calling function.
create or replace package pk
is
type e_list is table of emp%rowtype index by pls_integer;
function pro1 return e_list;
end;
create or replace package body pk
is
x number;
function pro1 return e_list
is
v_emp e_list;
begin
for x in 100..110 loop
select * into v_emp(x) from employees where employee_id=x;
end loop;
return v_emp;
end;
end;
to call function check below code plzz.
declare
type p_list is table of emp%rowtype index by pls_integer;
r_cur p_list;
x number;
begin
r_cur:=pk.pro1;
x:=r_cur.first;
for i in 1..10 loop
dbms_output.put_line(r_cur(x).salary);
x:=r_cur.next(x);
end loop;
end;
If you'll be using that type outside of the package, create it at SQL level. Something like this:
Sample data:
SQL> SELECT * FROM employees;
EMPLOYEE_ID ENAME SALARY
----------- ---------- ----------
100 SMITH 800
101 ALLEN 1600
102 WARD 1250
103 JONES 2975
Types:
SQL> CREATE OR REPLACE TYPE e_list_t IS OBJECT
2 (
3 employee_id NUMBER,
4 ename VARCHAR2 (20),
5 salary NUMBER
6 );
7 /
Type created.
SQL> CREATE OR REPLACE TYPE e_list IS TABLE OF e_list_t;
2 /
Type created.
Package specification and its body:
SQL> CREATE OR REPLACE PACKAGE pk
2 IS
3 FUNCTION pro1
4 RETURN e_list;
5 END;
6 /
Package created.
SQL> CREATE OR REPLACE PACKAGE BODY pk
2 IS
3 FUNCTION pro1
4 RETURN e_list
5 IS
6 v_emp e_list := e_list ();
7 BEGIN
8 SELECT e_list_t (employee_id, ename, salary)
9 BULK COLLECT INTO v_emp
10 FROM employees;
11
12 RETURN v_emp;
13 END;
14 END;
15 /
Package body created.
Testing:
SQL> DECLARE
2 r_cur e_list;
3 BEGIN
4 r_cur := pk.pro1;
5
6 FOR x IN 1 .. r_cur.LAST
7 LOOP
8 DBMS_OUTPUT.put_line (r_cur (x).ename || ': ' || r_cur (x).salary);
9 END LOOP;
10 END;
11 /
SMITH: 800
ALLEN: 1600
WARD: 1250
JONES: 2975
PL/SQL procedure successfully completed.
SQL>
First of all, I know the question is vague, so feel free to edit it if you can describe it better.
There are some sets of table like TEST_201812, TEST_201901, etc. . I have another table that stores these values:
TEST_DATE:
ID DATE
1 201810
2 201811
3 201812
4 201901
Now what I want is to select the tables mentioned above (e.g.TEST_201812) by using TEST_DATE. I know it's wrong, but something like this:
select * from TEST_(select DATE from TEST_DATE where ID = 1)
Does anyone know how to achieve this?
Seriously, such a data model is a disaster. If you want to keep separate months separately, use one - partitioned - table.
Anyway, here's one option of how to do that:
Sample tables and a function that returns refcursor:
SQL> create table test_201812 as select * From dept;
Table created.
SQL> create table test_date (id number, datum number);
Table created.
SQL> insert into test_date values (1, 201812);
1 row created.
SQL> create or replace function f_test (par_id in test_date.id%type)
2 return sys_refcursor
3 is
4 l_datum test_date.datum%type;
5 l_str varchar2(200);
6 l_rc sys_refcursor;
7 begin
8 select datum
9 into l_datum
10 from test_date
11 where id = par_id;
12
13 l_str := 'select * from test_' || l_datum;
14 open l_rc for l_str;
15 return l_rc;
16 end;
17 /
Function created.
Testing:
SQL> select f_test(1) from dual;
F_TEST(1)
--------------------
CURSOR STATEMENT : 1
CURSOR STATEMENT : 1
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL>
I have developed a procedure that adds a consultant to a table.
I would like to add a procedure to the consultant ID so that everytime a consultant gets added to the table a generic id is added to the record.
How can I create this procedure?
create or replace PROCEDURE ADD_CONSULTANT
( p_con_id LDS_CONSULTANT.CONSULTANT_ID%type,
p_con_name LDS_CONSULTANT.CST_NAME%type,
p_con_start LDS_CONSULTANT.START_DATE%type,
p_con_end LDS_CONSULTANT.LEAVE_DATE%type,
p_con_loc LDS_CONSULTANT.LOCATION%type,
p_con_spec LDS_CONSULTANT.SPECIALIST_AREA%type)
IS
BEGIN
INSERT INTO LDS_CONSULTANT (CONSULTANT_ID, CST_NAME, START_DATE, LEAVE_DATE, LOCATION, SPECIALIST_AREA)
VALUES (p_con_id, p_con_name, p_con_start, p_con_end, p_con_loc, p_con_spec);
END;
You didn't mention which database version you use, so the answer & the solution you choose might differ.
A simple solution is to use a sequence. Here's an example. First, a table:
SQL> CREATE TABLE lds_consultant (
2 consultant_id NUMBER,
3 cst_name VARCHAR2(20),
4 start_date DATE,
5 leave_date DATE,
6 location VARCHAR2(20),
7 specialist_area VARCHAR2(20)
8 );
Table created.
A sequence:
SQL> CREATE SEQUENCE seq_cons;
Sequence created.
A procedure; I removed parameter which you originally used, as you don't need it any more.
SQL> CREATE OR REPLACE PROCEDURE add_consultant (
2 p_con_name lds_consultant.cst_name%TYPE,
3 p_con_start lds_consultant.start_date%TYPE,
4 p_con_end lds_consultant.leave_date%TYPE,
5 p_con_loc lds_consultant.location%TYPE,
6 p_con_spec lds_consultant.specialist_area%TYPE
7 )
8 IS
9 BEGIN
10 INSERT INTO lds_consultant (
11 consultant_id,
12 cst_name,
13 start_date,
14 leave_date,
15 location,
16 specialist_area
17 ) VALUES (
18 seq_cons.NEXTVAL, --> this
19 p_con_name,
20 p_con_start,
21 p_con_end,
22 p_con_loc,
23 p_con_spec
24 );
25 END;
26 /
Procedure created.
Let's test it:
SQL> BEGIN
2 add_consultant('Littlefoot',trunc(SYSDATE),NULL,'Croatia','IT');
3 END;
4 /
PL/SQL procedure successfully completed.
SQL> SELECT * FROM lds_consultant;
CONSULTANT_ID CST_NAME START_DATE LEAVE_DATE LOCATION SPECIALIST_AREA
------------- -------------------- ---------- ---------- -------------------- --------------------
1 Littlefoot 13.05.2018 Croatia IT
SQL>
Another option is a database trigger:
SQL> CREATE OR REPLACE TRIGGER trg_bi_cons BEFORE
2 INSERT ON lds_consultant
3 FOR EACH ROW
4 BEGIN
5 :new.consultant_id := seq_cons.nextval;
6 END;
7 /
Trigger created.
Unlike the previous example, you don't even need to reference the consultant_id any more as trigger does that job.
SQL> CREATE OR REPLACE PROCEDURE add_consultant (
2 p_con_name lds_consultant.cst_name%TYPE,
3 p_con_start lds_consultant.start_date%TYPE,
4 p_con_end lds_consultant.leave_date%TYPE,
5 p_con_loc lds_consultant.location%TYPE,
6 p_con_spec lds_consultant.specialist_area%TYPE
7 )
8 IS
9 BEGIN
10 INSERT INTO lds_consultant (
11 cst_name,
12 start_date,
13 leave_date,
14 location,
15 specialist_area
16 ) VALUES (
17 p_con_name,
18 p_con_start,
19 p_con_end,
20 p_con_loc,
21 p_con_spec
22 );
23
24 END;
25 /
Procedure created.
Testing:
SQL> BEGIN
2 add_consultant('Bigfoot',trunc(SYSDATE),NULL,'France','Fashion');
3 END;
4 /
PL/SQL procedure successfully completed.
SQL> SELECT * FROM lds_consultant;
CONSULTANT_ID CST_NAME START_DATE LEAVE_DATE LOCATION SPECIALIST_AREA
------------- -------------------- ---------- ---------- -------------------- --------------------
1 Littlefoot 13.05.2018 Croatia IT
2 Bigfoot 13.05.2018 France Fashion
SQL>
If you're on 12c database version, use the identity column:
CREATE TABLE lds_consultant (
consultant_id NUMBER GENERATED BY DEFAULT ON NULL AS IDENTITY,,
cst_name VARCHAR2(20),
start_date DATE,
leave_date DATE,
location VARCHAR2(20),
specialist_area VARCHAR2(20)
);
The procedure would look like the one in the second (trigger) example, i.e. you don't need to reference the consultant_id column. As I run 11g XE on my laptop, I can't post the execution, but I'm sure you can do it yourself if necessary (and , of course, if you're on 12c).
I have PL/SQL function looking like:
FUNCTION get_agent_statistics ( id NUMBER
RETURN agent_stats_t
PIPELINED;
And I select from it (iBatis code):
SELECT * FROM table(pkg.get_agent_statistics(#id#))
How should I change this select if I'll remove PIPELINED statement from function?
If you'll get working compiled procedure without PIPELINED statement, you don't need to change your SELECT. See this - http://www.oracle-base.com/articles/misc/pipelined-table-functions.php
When you remove PIPELINED clause from the function declaration, function ceases to be a PIPELINED table function and as a result you will have to modify the body of the function to convert it to a TABLE function, if you still want to use it the from clause of the query, or a simple function, which you wont be able to use in the from clause of a query.
Addendum
Could I select something from non-pipelined function?
Yes, if you have a TABLE function, otherwise no. Here is a couple of examples:
-- prerequisites
SQL> create or replace type T_rows as object(
2 e_name varchar2(21),
3 e_lname varchar2(21)
4 )
5 /
Type created
SQL> create or replace type T_tab is table of t_rows
2 /
Type created
-- PIPELINED TABLE function
SQL> create or replace function GetEnames
2 return T_Tab
3 pipelined
4 is
5 l_etab t_tab := t_tab();
6 begin
7 for i in (select first_name
8 , last_name
9 from employees)
10 loop
11 pipe row(t_rows(i.first_name, i.last_name));
12 --l_etab.extend;
13 --l_etab(l_etab.last) := t_rows(i.first_name, i.last_name);
14 end loop;
15 return ;--l_etab;
16 end;
17 /
Function created
SQL> select *
2 from table(getenames)
3 where rownum <= 5;
E_NAME E_LNAME
--------------------- ---------------------
Steven King
Neena Kochhar
Lex De Haan
Alexander Hunold
Bruce Ernst
-- non-pipelined table function
SQL> create or replace function GetEnames
2 return T_Tab
3
4 is
5 l_etab t_tab := t_tab();
6 begin
7 for i in (select first_name
8 , last_name
9 from employees)
10 loop
11 --pipe row(t_rows(i.first_name, i.last_name));
12 l_etab.extend;
13 l_etab(l_etab.last) := t_rows(i.first_name, i.last_name);
14 end loop;
15 return l_etab;
16 end;
17 /
Function created
SQL> select *
2 from table(getenames)
3 where rownum <= 5;
E_NAME E_LNAME
--------------------- ---------------------
Steven King
Neena Kochhar
Lex De Haan
Alexander Hunold
Bruce Ernst
SQL>
I am a noobie in pl/sql. I am trying t calculate gpa using pl sql. i have created a table with grades with values in it.
SSN CNO GRADE
--------------- -------- ----------
55555 cs101 1
55555 math101 4
55555 bio101 1
55555 cgdd101 3
55555 swe203 3
55555 eng101 3
11111 bio101 4
11111 cgdd101 4
55555 cs101 1
55555 math101 4
55555 bio101 1
I am trying to calculate gpa with the following pl sql function but I get the following error.
55555 eng101 3
36 rows selected.
SQL> create or replace function get_count
2 return is
3 declare
4 v_count number;
5 begin
6 select count(*) into v_count from grade;
7 return grade;
8
9 end;
10 /
Warning: Function created with compilation errors.
SQL> show errors
Errors for FUNCTION GET_COUNT:
LINE/COL ERROR
-------- -----------------------------------------------------------------
2/8 PLS-00103: Encountered the symbol "IS" when expecting one of the
following:
<an identifier> <a double-quoted delimited-identifier> self
long double ref char time timestamp interval date binary
national character nchar
Your function is missing a return data type, e.g.:
create or replace function get_count
return NUMBER is
v_count number;
...
Also, you don't need the declare.
Try This
CREATE OR REPLACE FUNCTION GET_COUNT
RETURN NUMBER
IS
V_COUNT NUMBER;
BEGIN
SELECT COUNT (*)
INTO V_COUNT
FROM GRADE;
RETURN V_COUNT;
END;