问题
Let's take an example from .NET MVC where it is possible to define ActionResult with a parameter that is custom object or even an array of such objects:
Method:
public ActionResult Blah(Person[] people) {
// ...
}
and corresponding HTML:
<input type="text" name="people[0].FirstName" value="George" />
<input type="text" name="people[0].LastName" value="Washington" />
<input type="text" name="people[1].FirstName" value="Abraham" />
<input type="text" name="people[1].LastName" value="Lincoln" />
<input type="text" name="people[3].FirstName" value="Thomas" />
<input type="text" name="people[3].LastName" value="Jefferson" />
Example taken from this blog
I want to know if it is possible to do something similar in PL SQL, like:
PROCEDURE blah(param1 IN Person_type)
IS
BEGIN
-- do whatever y like
END;
where person_type defines object, record or similar complex structure.
回答1:
CREATE OR REPLACE TYPE t_parsed_name as object (
name_prefix varchar2(50),
name_first varchar2(50),
name_middle varchar2(50),
name_last varchar2(100),
name_suffix varchar2(50)
);
CREATE OR REPLACE TYPE T_PARSED_NAME_ARY as table of t_parsed_name;
create or replace procedure test_parsed_names(i_parsed_name_ary in t_parsed_name_ary) as
idx pls_integer;
begin
idx := i_parsed_name_ary.first;
loop
exit when idx is null;
dbms_output.put_line('First name: ' || i_parsed_name_ary(idx).name_first || ', Last name: ' || i_parsed_name_ary(idx).name_last);
idx := i_parsed_name_ary.next(idx);
end loop;
end;
----------------------------------------
-- example using above procedure
declare
l_parsed_name t_parsed_name;
l_parsed_name_ary t_parsed_name_ary;
begin
l_parsed_name_ary := t_parsed_name_ary();
l_parsed_name_ary.extend(3);
-- create some parsed names and add to array
l_parsed_name := t_parsed_name('Mr','Joe','T','Blow','Jr');
l_parsed_name_ary(1) := l_parsed_name;
l_parsed_name := t_parsed_name('Mrs','Jane','','Doe','');
l_parsed_name_ary(2) := l_parsed_name;
l_parsed_name := t_parsed_name('','Betty','','Boop','');
l_parsed_name_ary(3) := l_parsed_name;
-- test the array (call procedure with object type array parameter)
test_parsed_names(l_parsed_name_ary);
end;
回答2:
Yes you may, but you will need to use ODP and Oracle's UDTs (User Defined Type). Not 100% sure how to map these to the MVC bit but you may pass object types to/from .net to Oracle.
Check out these links: Calling a function with user defined type parameters (Oracle ODP.NET) (with specific attention to this walkthrough http://st-curriculum.oracle.com/obe/db/hol08/dotnet/udt/udt_otn.htm )
http://docs.oracle.com/html/E15167_01/featUDTs.htm
it will probably take some fine-tuning on your part to get everything to sync, but it is possible.
来源:https://stackoverflow.com/questions/12177025/is-it-possible-to-pass-objects-to-pl-sql-package-procedure-as-parameters