How to populate specific data from database into list item? - oracle

I created ListItem1 with List Style (Poplist) for department no (10,20,30) and other ListItem2 with List Style (Tlist) for employees name. Record group for employees name
When i click on department no.10 then populate all employees name data into ListItem2. I want to populate data only employees names who falls under department no.10 or 20 or 30
CODE:
DECLARE
a VARCHAR2(100);
num NUMBER := 10;
BEGIN
a := populate_group ('R1');
populate_list ('LIST1','R1');
END;
"LIST1" for populate employees name data
"R1" is a group record name

Sounds like you just need to add where department_no = :List1 to the R1 record group.

In PL/SQL lists are called collections.
Try something like it:
DECLARE
TYPE population_type IS TABLE OF NUMBER INDEX BY VARCHAR2(64);
country_population population_type;
continent_population population_type;
howmany NUMBER;
which VARCHAR2(64)
BEGIN
country_population('Greenland') := 100000;
country_population('Iceland') := 750000;
howmany := country_population('Greenland');
continent_population('Australia') := 30000000;
continent_population('Antarctica') := 1000; -- Creates new entry
continent_population('Antarctica') := 1001; -- Replaces previous
value
which := continent_population.FIRST; -- Returns 'Antarctica'
-- as that comes first alphabetically.
which := continent_population.LAST; -- Returns 'Australia'
howmany := continent_population(continent_population.LAST);
-- Returns the value corresponding to the last key, in this
-- case the population of Australia.
END;

Related

inserting of data not happening in oracle

I am new to plsql.I have a table where i need to insert the data(some dummy data).So,i thought to use plsql block and using For loop it will insert the data automatically.The plsql block is runned successfully,but the data are stored as empty.The block I tried is:
declare
v_number1 number;
v_number2 number;
v_number3 number;
begin
For Lcntr IN 2..17
LOOP
v_number1 := v_number1+1;
v_number2 := v_number2+2;
v_number3 := v_number3+3;
Insert into stu.result(res_id,stu_id,eng,maths,science) values (stu.seq_no.NEXTVAL,Lcntr,v_number1,v_number2,v_number3);
END LOOP;
end;
But my table is loaded as:(please ignore first two row data,i inserted it manually):
The data for eng,maths,science is not being inserted.why it is happening so?
That's because your variables are NULL. NULL + 1 = NULL as well.
If you modify declaration to
v_number1 number := 0;
v_number2 number := 0;
v_number3 number := 0;
something might happen.

In PL/SQL can I set a variable dynamically?

In Oracle 12c R2, I have a function which receives a row type as a variable. In the function I want to read a table which contains a column name and a value, I want to then populate the row type variable passed in using the column name and the data from the table I read.
Here is a simplistic idea of what I want to do;
CREATE TABLE table_to_be_updated
(
key_value number,
cola varchar2(2),
colb varchar2(2),
colc varchar2(2),
cold varchar2(2),
cole varchar2(2),
colf varchar2(2)
);
CREATE TABLE table_default_value
(
default_stuff number,
column_name varchar(30),
column_default_value varchar2(2)
);
function do_defaults(in_table table_to_be_updated%rowtype, in_value number) return table_to_be_updated%rowtype
is
out_table table_to_be_updated%rowtype := in_table;
cursor my_curs
is
select * from table_default_value where default_stuff = in_value;
begin
for default_rec in my_curs
loop
out_table.[default_rec.column_name] := default_rec.column_default_value
end loop;
return out_table;
end;
insert into table_default_value (default_stuff,column_name,column_default_value) values (1,'cola','xx'));
insert into table_default_value (default_stuff,column_name,column_default_value) values (1,'colc','aa'));
insert into table_default_value (default_stuff,column_name,column_default_value) values (1,'cole','bb'));
In the line;
out_table.[default_rec.column_name] := [default_rec.column_default_value]
[default_rec.column_name] would be the column name, from the cursor, in out_table name I want to move data to.
and
[default_rec.column_default_value] is the value from the cursor I want to move into that column.
I suspect that what I want to do is impossible in PL/SQL, but I thought I'd ask.
There are other ways to accomplish updating the table directly, specifically using dynamic SQL with execute immediate, but I have a number of similar tables which all need to have the same things done to them, and I would prefer a single function to work on a record and then pass it back to have the calling routine update the proper table.
Here is the best I can come up with;
function do_defaults(in_table table_to_be_updated%rowtype, in_value number) return table_to_be_updated%rowtype
is
TYPE DEFAULT_TYPE IS TABLE OF VARCHAR2(2)
INDEX BY VARCHAR2(30);
DEFAULT_ARRAY DEFAULT_TYPE;
out_table table_to_be_updated%rowtype := in_table;
cursor my_curs
is
select * from table_default_value where default_stuff = in_value;
begin
DEFAULT_ARRAY('cola') := null;
DEFAULT_ARRAY('colb') := null;
DEFAULT_ARRAY('colc') := null;
DEFAULT_ARRAY('cold') := null;
DEFAULT_ARRAY('cole') := null;
DEFAULT_ARRAY('colf') := null;
for default_rec in my_curs
loop
DEFAULT_ARRAY(default_rec.column_name) := default_rec.column_default_value
end loop;
out_table.cola := DEFAULT_ARRAY('cola');
out_table.colb := DEFAULT_ARRAY('colb');
out_table.colc := DEFAULT_ARRAY('colc');
out_table.cold := DEFAULT_ARRAY('cold');
out_table.cole := DEFAULT_ARRAY('cole');
out_table.colf := DEFAULT_ARRAY('colf');
return out_table;
end;

Fetching data dynamically in Collections

I need to use collections as below, but want to print the output dynamically instead of specifying PL01, PL02, PL03...as this is going to be bigger list. If not possible using RECORD type, how can this be achieved using collections.
Business scenario : Having a item price list in an excel sheet as price list code in the first row (each column refers to a price list) and different items in each row. The number of price list and the items will differ every time. Now i need to populate this excel in collections in same format (Table with items in rows and price list code as columns) and use it to update the correct price list.
DECLARE
TYPE RT IS RECORD (ITEM VARCHAR2(20),
PL01 NUMBER,
PL02 NUMBER,
PL03 NUMBER,
PL04 NUMBER,
PL05 NUMBER);
TYPE TT IS TABLE OF RT;
MY_REC RT;
MY_TAB TT := TT();
BEGIN
MY_TAB.EXTEND;
MY_TAB(1).ITEM := 'ABC';
MY_TAB(1).PL01 := '40';
MY_TAB(1).PL02 := '42';
MY_TAB(1).PL03 := '44';
MY_TAB(1).PL04 := '46';
MY_TAB(1).PL05 := '48';
MY_TAB.EXTEND;
MY_TAB(2).ITEM := 'DEF';
MY_TAB(2).PL01 := '60';
MY_TAB(2).PL02 := '62';
MY_TAB(2).PL03 := '64';
MY_TAB(2).PL04 := '66';
MY_TAB(2).PL05 := '68';
FOR I IN 1..2
LOOP
Dbms_Output.PUT_LINE(MY_TAB(I).ITEM||' - '||MY_TAB(I).PL01||' - '||MY_TAB(I).PL02||' - '||
MY_TAB(I).PL03||' - '||MY_TAB(I).PL04||' - '||MY_TAB(I).PL05);
END LOOP;
END;
/
One way is to create TYPE as objects and using a TABLE function to display by passing a REFCURSOR.
CREATE OR REPLACE TYPE RT AS OBJECT ( ITEM VARCHAR2(20),
PL01 NUMBER,
PL02 NUMBER,
PL03 NUMBER,
PL04 NUMBER,
PL05 NUMBER
);
/
CREATE OR REPLACE TYPE TT AS TABLE OF RT;
/
VARIABLE x REFCURSOR;
DECLARE
MY_TAB TT := TT();
BEGIN
MY_TAB.EXTEND(2); --allocate 2 elements
MY_TAB(1) := RT ( 'ABC',40,42,44,46,48);--you can assign all once/index
MY_TAB(2) := RT ( 'DEF',60,62,64,66,68);
OPEN :x FOR SELECT * FROM TABLE(MY_TAB);
END;
/
PRINT x
ITEM PL01 PL02 PL03 PL04 PL05
-------------------- ---------- ---------- ---------- ---------- ----------
ABC 40 42 44 46 48
DEF 60 62 64 66 68
You can use a PL/SQL table inside the RECORD. Try this:
DECLARE
TYPE PL_TABLE_TYPE IS TABLE OF NUMBER;
TYPE RT IS RECORD (ITEM VARCHAR2(20), PL_TABLE PL_TABLE_TYPE);
TYPE TT IS TABLE OF RT;
MY_TAB TT := TT();
BEGIN
MY_TAB.EXTEND;
MY_TAB(MY_TAB.LAST).ITEM := 'ABC';
MY_TAB(MY_TAB.LAST).PL_TABLE := PL_TABLE_TYPE(40,42,33,46,48);
--> Why do you assign strings like '40' if you have a NUMBER data type?
MY_TAB.EXTEND;
MY_TAB(MY_TAB.LAST).ITEM := 'DEF';
MY_TAB(MY_TAB.LAST).PL_TABLE := PL_TABLE_TYPE(60,62,64,66,68);
FOR r IN MY_TAB.FIRST..MY_TAB.LAST LOOP
DBMS_OUTPUT.PUT(MY_TAB(r).ITEM || ' ');
FOR i IN MY_TAB(r).PL_TABLE.FIRST..MY_TAB(r).PL_TABLE.LAST LOOP
DBMS_OUTPUT.PUT(MY_TAB(r).PL_TABLE(i) || ' ');
END LOOP;
DBMS_OUTPUT.NEW_LINE;
END LOOP;
END;

Creating function that returns table that isn't declared yet

Our product has several components that can be installed separately and supports oracle. But we do not grant Create type privilege. So during installation of one of the components, I need to ask customer to install the component, add Create type privilege and then run my component.
In the SQL file that will create functions I was planning to give following code:
CREATE OR REPLACE FUNCTION get_some_data (input INT)
RETURN my_table
AS
my_table_var my_table := my_table ();
ret_code INT := 0;
BEGIN
ret_code := create_my_type_and_table ();
IF 1 = ret_code
THEN
NULL; -- add some data to my_table_var here
END IF;
RETURN my_table_var;
END get_some_data;
/
The function create_my_type_and_table would use execute immediate to create record and table type.
Obviously, the problem is since function get_some_data says it will return my_table, compilation fails.
I wanted to know:
is there a way out?
the reason why I want to create and return table is because I need to return multiple fields. All of them are int. Is there a way I can return multi dimensional array, perhaps system collection? I tried sys.odcinumberlist but I did not find a way by which I can return 4 columned sys.odcinumberlist.
If you want some anonymous generic stuff, you could use TABLE OF [TYPE].
Here an example how to build a table of number which is used by another type "table of table of number".
Like every type, you can create them within your schema to use it between different plsql-blocks or return-value in functions.
declare
TYPE tableOfNumber is Table of Number; -- Define a row of numbers
TYPE tableOfTableOfNumer is Table of tableOfNumber; -- define a table of rows
tableMaster tableOfTableOfNumer := tableOfTableOfNumer(); -- init tables
tableChild1 tableOfNumber := tableOfNumber();
tableChild2 tableOfNumber := tableOfNumber();
begin
tableChild1.Extend; -- add a new number-field to our table
tableChild1(1) := 0; -- set the value to the new field
tableChild1.Extend;
tableChild1(2) := 1;
tableChild2.Extend;
tableChild2(1) := 2;
tableChild2.Extend;
tableChild2(2) := 3;
tableMaster.Extend; -- add a new 'row' to out table
tableMaster(1) := tableChild1; -- fill the new 'row' with the fields
tableMaster.Extend;
tableMaster(2) := tableChild2;
-- loop through our 'table'
for r in 1 .. tableMaster.Count
LOOP
for c in 1 .. tableMaster(r).Count
LOOP
dbms_output.put_line(tableMaster(r)(c));
END LOOP;
END LOOP;
end;
If you want to create a function you have to:
1. Declare types in you schema
(an grant rights to the users)
CREATE TYPE tableOfNumber AS TABLE OF NUMBER;
CREATE TYPE tableOftableOfNumber AS TABLE OF tableOfNumber;
2. Create you function:
(here with same code like the plsql-block)
CREATE OR REPLACE FUNCTION DINTER.MyFunction
RETURN tableOftableOfNumber
Is
tableMaster tableOftableOfNumber := tableOftableOfNumber(); -- init tables
tableChild1 tableOfNumber := tableOfNumber();
tableChild2 tableOfNumber := tableOfNumber();
begin
tableChild1.Extend; -- add a new number-field to our table
tableChild1(1) := 0; -- set the value to the new field
tableChild1.Extend;
tableChild1(2) := 1;
tableChild2.Extend;
tableChild2(1) := 2;
tableChild2.Extend;
tableChild2(2) := 3;
tableMaster.Extend; -- add a new 'row' to out table
tableMaster(1) := tableChild1; -- fill the new 'row' with the fields
tableMaster.Extend;
tableMaster(2) := tableChild2;
RETURN tableMaster;
end MyFunction;
/
3. Call the function:
declare
tableMaster tableOfTableOfNumber;
begin
tableMaster := myfunction();
-- loop through our 'table'
for r in 1 .. tableMaster.Count
LOOP
for c in 1 .. tableMaster(r).Count
LOOP
dbms_output.put_line(tableMaster(r)(c));
END LOOP;
END LOOP;
end;

Oracle Forms list does not display values

I want to make list which will display values, like text item but in list.
My code:
DECLARE
rg_dept RecordGroup;
rg_dname VARCHAR2(4) := 'Name';
dlist_ID Item := Find_Item('PROJESCT.LIST_ID');
nDummy NUMBER;
BEGIN
rg_dept := Find_Group(rg_dname);
-- Delete any existing Group first
IF NOT Id_Null(rg_dept) THEN
Delete_Group(rg_dept);
END IF;
-- Now create a Record Group using a SQL query
-- Your Query must have a Label and a Value (two Columns)
-- and the data types must match your item type
rg_dept := Create_Group_From_Query(rg_dname,'SELECT department_name, to_char(department_id) FROM departments');
--Clear the existing List
Clear_List(dlist_ID);
-- Populate the Record Group
nDummy := Populate_Group(rg_dept);
-- Populate the List Item
Populate_List(dlist_ID ,rg_dept);
END;
If I make list item and add this code, form will display nothing; if I remove list, all is ok.
P.S. Trigger: when-list-item-change
I should recommend you to populate your lists from e.g. When-new-Form-Instance. As usual you will get a good idea what you need to do from Forms help (menu 'Help/Online Help'). This is a procedure I have made to simple populate list only by specifying the name of the list item and the select statement to populate the lsit item with.
PROCEDURE populate_list_item (
p_item_name VARCHAR2
, p_select VARCHAR2
) IS
l_rg_id RECORDGROUP;
l_list_id ITEM;
l_err_num PLS_INTEGER;
FUNCTION create_temp_group (
p_select VARCHAR2
) RETURN RECORDGROUP IS
l_rg_id RECORDGROUP;
l_group_name VARCHAR2(30) := 'TMP$RG';
BEGIN
l_rg_id := FIND_GROUP(l_group_name);
--Make sure that record group don't alreay exist
IF NOT ID_NULL(l_rg_id) THEN
DELETE_GROUP(l_rg_id);
END IF;
--Populate the temporary record group
l_rg_id := CREATE_GROUP_FROM_QUERY(l_group_name, p_select);
RETURN l_rg_id;
END create_temp_group;
BEGIN
l_rg_id := create_temp_group(p_select);
l_err_num := Populate_Group(l_rg_id);
--Allow for no data found in the selection query
IF l_err_num NOT IN (0, 1403) THEN
RAISE Form_Trigger_Failure;
END IF;
l_list_id := Find_Item(p_item_name);
IF ID_NULL(l_list_id) THEN
RAISE Form_Trigger_Failure;
END IF;
Populate_List(l_list_id, l_rg_id);
Delete_Group(l_rg_id);
END populate_list_item;
The When-New-Form-Instance is a form level trigger and should be under the form (instead of block or item):
The best thing to do is to build all your record groups at design time. It does not slow down the performance unless you have some huge, already slow form. Then populate your list items at run time. Always Copy/paste the code from Forms help.
--Oracle Forms Example: Create a record group from a query, and populate it.
DECLARE
rg_name VARCHAR2(40) := 'Salary_Range';
rg_id RecordGroup;
errcode NUMBER;
BEGIN
/*
** Make sure group doesn't already exist
*/
rg_id := Find_Group( rg_name );
/*
** If it does not exist, create it and add the two
** necessary columns to it.
*/
IF Id_Null(rg_id) THEN
rg_id := Create_Group_From_Query( rg_name,
'SELECT SAL-MOD(SAL,1000) BASE_SAL_RANGE,'
||'COUNT(EMPNO) EMPS_IN_RANGE '
||'FROM EMP '
||'GROUP BY SAL-MOD(SAL,1000) '
||'ORDER BY 1');
END IF;
/*
** Populate the record group
*/
errcode := Populate_Group( rg_id );
END;

Resources