Skip to Main Content
  • Questions
  • using pl/sql to implement multiple option select

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, hxl.

Asked: March 31, 2004 - 2:34 pm UTC

Last updated: May 31, 2004 - 1:11 pm UTC

Version: 8.1.7

Viewed 1000+ times

You Asked

Tom,

Have you ever implemented multiple option select in pl/sql? I used to do it in php, which is quite straightforward: you take the choices and assign it to an array. I thought I should be able to do it in pl/sql too, using one of the collection types to take the list of values, so I tried the following:

create type multi_option_t as varray (10) of varchar2(1);
/

create or replace procedure test (multi_option IN multi_option_t default NULL,
p_submit IN varchar2 default NULL)
as
begin
if (p_submit = 'Submit') then
htp.p('foobar');
for i in 1 .. multi_option.count
loop
htp.p('value for the ' || i || 'th element is ' || multi_option(i));
end loop;
else
htp.p('<center><table width="98%"><tr>');
htp.p('<form method="post" action="test" name="testform">');
htp.p('<tr><td width="50%">Choose Status</td>');
htp.p(' <td width="50%">
<select name="multi_option" multiple>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
</select>
</td></tr>');
htp.p('<tr><td colspan="2"><input type="submit" name="p_submit" value="Submit"></td></tr>');

htp.p('</table></form>');
end if;
end test;
/


But it did not do the trick. It compiled ok but at run time, error msg I got:
ORA-06550: line 7, column 2:
PLS-00306: wrong number or types of arguments in call to 'TEST'
ORA-06550: line 7, column 2:
PL/SQL: Statement ignored

PARAMETERS :
============
MULTI_OPTION:
2
P_SUBMIT:
Submit

The parameters for the varray always just show the 1st choice. Seems it is not recognizing multi_option as type multi_option_t at all - it is still treated as a scalar parameter. Is varray not the right type to use for this, or it is something else?

Thanks a million!

and Tom said...

we use plsql table types for that (and use packages!! they rock)

ops$tkyte@ORA9IR2> create or replace package my_pkg
2 as
3 type array is table of varchar2(4000) index by binary_integer;
4 g_empty array;
5
6 procedure test( p_multi_option in array default g_empty,
7 p_submit in varchar2 );
8 end;
9 /

Package created.

ops$tkyte@ORA9IR2>
ops$tkyte@ORA9IR2> create or replace package body my_pkg
2 as
3
4 procedure test( p_multi_option in array default g_empty,
5 p_submit in varchar2 )
6 as
7 begin
8 if (p_submit = 'Submit')
9 then
10 htp.p('foobar');
11 for i in 1 .. p_multi_option.count
12 loop
13 htp.p('value for the ' || i || 'th element is ' || p_multi_option(i));
14 end loop;
15 else
16 htp.p('<center><table width="98%"><tr>');
17 htp.p('<form method="post" action="test" name="testform">');
18 htp.p('<tr><td width="50%">Choose Status</td>');
19 htp.p(' <td width="50%">
20 <select name="p_multi_option" multiple>
21 <option value="0">0</option>
22 <option value="1">1</option>
23 <option value="2">2</option>
24 <option value="3">3</option>
25 <option value="4">4</option>
26 <option value="5">5</option>
27 <option value="6">6</option>
28 <option value="7">7</option>
29 <option value="8">8</option>
30 <option value="9">9</option>
31 </select>
32 </td></tr>');
33 htp.p('<tr><td colspan="2"><input type="submit" name="p_submit" value="Submit"></td></tr>');
34
35 htp.p('</table></form>');
36 end if;
37 end test;
38
39 end;
40 /

Package body created.


Rating

  (2 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

You rock too!

Pat, March 31, 2004 - 4:17 pm UTC

Thanks so much.

To other readers, there are a few typos in the code, like p_submit needs to be default null in package header, but you get the idea.

Charles Mafra, May 31, 2004 - 4:54 am UTC

Hi Tom,



You use just htp.p procedure in this example

htp.p('<tr><td width="50%">Choose Status</td>');

... why don't use commands
like the web toolkit reference, I mean htp.tableRowOpen,
htp.tableData and so on... what is best?
What is fast? Why? Can you give me a simple example?
I just don't know the best thing to do I am new in web developer came from Forms/Reports 6i.

Thanks for all


Tom Kyte
May 31, 2004 - 1:11 pm UTC

I know html. I just chose not to use the other more verbose calls.

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library