English 中文(简体)
How to test an Oracle function that contains DML in PLSQL Developer?
原标题:

Selecting the return value of an Oracle stored function that doesn t contain DML can be done by simply selecting the function:

select function_name() from dual;

If the function contains DML (in this case some inserts to log the arguments passed to the function), the above query is not allowed. (ORA-14551)

How can I select/view the return value of this function?

if I choose "test" in plsql developer, plsqldev produces something like:

declare
  -- Non-scalar parameters require additional processing 
  result xmltype;
begin
  -- Call the function
  result := find_person(as_surname => :as_surname,
                       as_given => :as_given,
                       ad_birth_date_from => :ad_birth_date_from,
                       ad_birth_date_to => :ad_birth_date_to,
                       as_gender => :as_gender);
end;

How can I view the value of the "result" variable?

select result from dual;

inside the begin/end block produces

ORA-06550: PLS-00428: an INTO clause is expected in this SELECT statement
问题回答

change "result" to ":result" and click on the little arrow thingy in the top left corner of the variables grid. It should add "result" as a bind varibale and you can specify its type.

In your case the best options are clob or PL/SQL string.

And your script could look like so:

declare
  result xmltype;
begin
  result := find_person(as_surname => :as_surname,
                        as_given => :as_given,
                        ad_birth_date_from => :ad_birth_date_from,
                        ad_birth_date_to => :ad_birth_date_to,
                        as_gender => :as_gender);
  if result is null then
    :result := null;
  else
    :result := result.GetClobVal();
  end if;
end;

As you can see, it is basically what PL/SQL Dev has created for you, except for the handling of how to return the xmltype in a way that PL/SQL Dev understands.

if you want to return a resultset, you can return cursors:

begin
  ...
  open :someCursor for 
    select 1 from ...;
  ...

You have to change the type of "someCursor" in the variables grid to "cursor" or it is not going to work.

I haven t worked with xmltype, but documentation gives the following option:

dbms_output.put_line(result.getStringVal());

adding

pragma autonomous_transaction 

to the function in the declare block allows it to be selected from dual

select find_person(arguments) from dual;

Since the DML in the function is simply for logging the arguments passed in to the function, it is an acceptable use of autonomous_transaction, but otherwise should be avoided

pragma_autonomous_transaction is one way.

but to test with out effecting your original database, there are few open source tools to test your SQL / PLSQLs like DBUNIT, utPLSQL etc.

these are unit testing tools for SQL and plsql

The test screen in PLSQL developer has two sections. In the upper part you will find the code you ve shown in your question. The code that the test function has generated has replaced the variables of your function with bind variables: :as_surname, :as_given etc. In the lower part of the screen you can enter values for these parameters and view the value of the result.

SQL Developer works pretty well with DBMS_OUTPUT .. it has a separate tab for it alongside the script output, results etc. just click the little "enable output" button and use DBMS_Output.Put_Line(result); in your code.





相关问题
what is wrong with this mysql code

$db_user="root"; $db_host="localhost"; $db_password="root"; $db_name = "fayer"; $conn = mysqli_connect($db_host,$db_user,$db_password,$db_name) or die ("couldn t connect to server"); // perform query ...

Users asking for denormalized database

I am in the early stages of developing a database-driven system and the largest part of the system revolves around an inheritance type of relationship. There is a parent entity with about 10 columns ...

Easiest way to deal with sample data in Java web apps?

I m writing a Java web app in my free time to learn more about development. I m using the Stripes framework and eventually intend to use hibernate and MySQL For the moment, whilst creating the pages ...

join across databases with nhibernate

I am trying to join two tables that reside in two different databases. Every time, I try to join I get the following error: An association from the table xxx refers to an unmapped class. If the ...

How can I know if such value exists in database? (ADO.NET)

For example, I have a table, and there is a column named Tags . I want to know if value programming exists in this column. How can I do this in ADO.NET? I did this: OleDbCommand cmd = new ...

Convert date to string upon saving a doctrine record

I m trying to migrate one of my PHP projects to Doctrine. I ve never used it before so there are a few things I don t understand. In my current code, I have a class similar to this: class ...

热门标签