PL/SQL Dynamic content is not displaying in Oracle Apex - oracle

I am trying to display the results of a query in a region. However, the region just displays "New" and not the actual output. Could you please help?
PL/SQL code:
DECLARE
V_APP_COUNT NUMBER;
BEGIN
SELECT COUNT(*) INTO V_APP_COUNT FROM APPLICATION;
DBMS_OUTPUT.PUT_LINE(V_APP_COUNT);
END;

Query you posted suits better to a report (pick any you want - interactive, classic or grid), not to PL/SQL Dynamic Content. It expects from you to actually "draw" the page using HTML code. Basically, your PL/SQL would use htp.p calls in order to display the result.
As I said - use a report.

Related

Oracle Reports - How to Capture Page Number and Insert to Database?

I have fields linked to page number and total page number. And that's pretty simple.(Image below)
But I also want to capture the page number in "BETWEEN PAGES " trigger and insert into database.(image below)
Please help. Thanks!
Getting the current page number is trivial; use SRW built-in package, its GET_PAGE_NUM function. For example:
declare
l_page_num number;
begin
srw.get_page_num (l_page_num);
insert into some_table (page_number) values (l_page_num);
end;

How to create view using dynamic action in APEX oracle?

I created a procedure to create a view dynamically using a shuttle item in APEX that returns the colon separated values. Here's the code to create it.
create or replace procedure create_temporary_view (input_employees in varchar2) is
begin
execute immediate 'create or replace view temp_batch_id as with emps(shuttle_item) as
(select '''||input_employees||''' from dual)
select regexp_substr(shuttle_item, ''[^:]+'', 1, level) batch_id from emps connect by level <= regexp_count(shuttle_item, '':'') + 1;';
commit;
end;
And inside the Execute PL/SQL code dynamic action i added the code something like
begin
create_temporary_view(:P12_EMPLOYEES);
end;
where :P12_EMPLOYEES is the shuttle item giving the colon separated values.
Apparently this piece of code works when i do it from the SQL Commands tab in Oracle APEX but the procedure isn't getting called from the EXECUTE PL/SQL code dynamic action. Is there a specific reason for it? If yes, what is it? If not, what could be the possible errors? Thanks in Advance.
I'm not sure what's your final goal, but I wouldn't create any table or view from APEX like that.
I think you actually want to populate a collection.
Have a look at how to create/populate collection in APEX and I think you'll find whatever you're trying to do.
It looks like you're trying to tokenize your P12_EMPLOYEES variable. This is not the right way to do this. Apex has a built-in PL/SQL function for doing this:
apex_string.split( :P12_EMPLOYEES, ':' )
this will give you a apex_t_varchar2 collection, split by colons.
Why your approach is a bad idea, consider what happens if two users are using your application at the same time. You'll create a view visible to both users with data from only one.

Construct and debug a PL/SQL simple or complex select view statement in a procedure

How do I perform a select on a rather simple view in oracle pl/sql using a stored procedure.
Lets say the view looks like this:
FirstName LastName
-------- -------
Bob Jones
James Kay
etc...
To me its should be so simple:
Procedure SuperSimple()
begin
select FirstName, LastName from SuperSimple
end
However I've been told that this will not work.
So I tried to use a PL/SQL cursor. Still scratching my head trying to figure out why I am using cursors. But it appears to be necessary in 11g.
Procedure AlphaPrime(Results OUT Ref CURSOR) IS
begin
OPEN Results for
select FirstName, LastName from SuperSimple;
end;
Now I was hoping this would work but I'm doing something like this with select statements and it appears to be not working.
Do I also need to add a fetch and another open and a close command to make this thing work? What is the idea behind all this? I've noticed that trying to find info on how to add a very simple select statemetn to a procedure appears to be missing from most documentation that I've read. Is there a reason for this like its too simple to add a select statement to a procedure as it would be better to add it to a view. Something along those lines.
The problem I'm having is I want to start out really simple and tac on a little bit more complexity to the sproc over time... where time ~ 1 to 2 hours. Can someone point me to some docs in Oracle PL/SQL that shows how to add a simple table or view. Also If the permissions for a specific view or table is not allowed does it just fail for that user or does it give an empty result set.
It is not clear from your question what are you intending to do with the query result inside your procedure. So here I make some examples with dbms_output which prints to screen out some message and data from your query. Probably you will replace it with your logic.
Let's have some view (actually it doesn't matter here whether you are querying view or table, but I would stick to your question)
create table some_simple_table(firstname varchar2(30), lastname varchar2(30));
/
create or replace view supersimple_view as select firstname, lastname, 'whatever1' whatever from some_simple_table;
/
The following code does select into variable, this will work only if query returns exactly one row.
create or replace procedure supersimple1 is
vfirstname supersimple_view.firstname%type;
vwhatever supersimple_view.whatever%type;
vsupersimple supersimple_view%rowtype;
begin
select firstname, whatever into vfirstname, vwhatever from supersimple_view;
dbms_output.put_line('I''m doing some logic with this'|| vwhatever );
select * into vsupersimple from supersimple_view;
dbms_output.put_line('I''m doing some logic with this'|| vsupersimple.firstname);
end;
/
Perhaps you can implement implicit cursor loop through results and do some logic.
create or replace procedure supersimple2 is
begin
for rec in (select * from supersimple_view)
loop
dbms_output.put_line('I''m doing some logic with this record '|| rec.firstname);
end loop;
end;
/
Another option is cursor (particularly in case when you will reuse the same select) loop through results and do some logic.
create or replace procedure supersimple3 is
cursor cur is (select * from supersimple_view);
vsupersimple cur%rowtype;
begin
open cur ;
loop
FETCH cur INTO vsupersimple;
EXIT WHEN cur%NOTFOUND;
dbms_output.put_line('I''m doing some logic with this record '|| vsupersimple.firstname);
end loop;
close cur;
end;
/
You can fetch result of your query to collection
create or replace procedure supersimple4 is
type supersimple_colt is table of supersimple_view%rowtype index by pls_integer;
vsupersimple_col supersimple_colt;
begin
select * bulk collect into vsupersimple_col from supersimple_view ;
for i in 1 .. vsupersimple_col.count
loop
dbms_output.put_line('I''m doing some logic with this record '|| vsupersimple_col(i).firstname);
end loop;
end;
/
Instead of PL/SQL type declared in supersimple4 you can create standalone database SQL types and used them to fetch results into. This aproach gives you various features like: possibility to query collection in select statement in table like fashion, converting it to xml by xmltype, etc.
I think I found the answer. For each column that is selected on, it needs a view or table column type, which is sort of like the list of parameters used for the final output. That way when you declare on it you can better know what you are getting, which sorta makes sense.
So if you have two tables or views which were used to generate the output columns, you would need both of those tables or views in your descriptive OUT variables to describe better what you are outputting in the final output result.
See this link.
I'm taking an educated guess with this next part as I'm just beginning to understand it:
This query should work. But if its not it may be due to insuffiecient priviledges. Try a table that you know you have access and select it in a procedure in debug mode. Then try a view.
Procedure AlphaPrime(Results OUT Ref CURSOR) IS
begin
OPEN Results for
select FirstName, LastName from SuperSimple;
end;
Also there is a possibility with Debug mode and your assigned user roles that you may have insufficient priviledges to debug all the objects in the view and they may not be accessible. Sometimes you can just hit the "Ignore" button in Toad to skip over debugging inside a stored procedure. Also you may have priveledges to view the results the object just not view its structure which may also give you insufficient priviledges errors. Again just ignore them to skip over those types of issues and see the results while in debug mode. If you don't debug this, then you should not see any errors and just get the results.

Validating a PL/SQL Stored Procedure OUT REF CURSOR in SQL Developer's Unit Testing framework

I am developing a series of PL/SQL Stored Procedures in a package that open REF CURSORs that are passed to the caller as OUT parameters for purposes of interoperability with another framework. I am using SQL Developer and recently started looking into using the testing features built into it.
I am looking for a way to validate the results of a REF CURSOR using SQL Developer's Unit Testing framework. I developed some paging logic, and I want to ensure that it is operating correctly so I was hoping I could verify that I have the correct number of rows and that their row numbers are within the range of the appropriate page.
I found a similar question here, but that solution renders the unit tests absolutely useless (at least as far as I can tell) because it doesn't actually perform any validation after running the procedure. If someone could explain how to validate it or show me what I'm missing from the solution from Oracle's Forums, I would appreciate it.
A Process Validation can be used to unit test datatypes that SQL Developer cannot handle.
Create an implementation and link (synchronize) it with a dummy procedure or function.
Set up parameters and results so that the Test completes with a Success
Create a Process Validation with 'User Pl/Sql Code' using the template shown
Template:
DECLARE
l_Cursor SYS_REFCURSOR ;
BEGIN
OPEN l_Cursor FOR '<SQL goes here>' ;
MY_PROCEDURE( l_Cursor ) ;
<do validation>
CLOSE l_Cursor;
IF <not valid> THEN
RAISE PROGRAM_ERROR ;
END IF ;
END ;
We have done something similar in PL/SQL (although not with the Unit Testing Framework of SQL Developer) by comparing the XML representations of two REF CURSORs:
define a "expected" query that returns your expected output as a REF CURSOR (typically selecting some constants from dual)
run both your "expected" and your "real" query, and convert their output to XML / HTML / whatever
compare the output
To convert a REF CURSOR to XML, you can use DBMS_XMLGEN, as shown in AskTom on describing a ref cursor

Oracle SQL Developer: Show REFCURSOR Results in Grid?

As a follow-up to the question "Get resultset from oracle stored procedure", is there a way to show the results of a stored procedure that returns a REFCURSOR in a grid (instead of the plain text Script Output window) within SQL Developer?
EDIT: The answer helped, but I'm still having a problem displaying the result set in the "View Value" window:
The columns can only be expanded a small amount, probably due to the number of results being returned. Expanding the window with the resizer control doesn't help:
I don't think you can with a procedure.
Edit: Thanks to DCookie for simplifying my original answer.
But as a work-around you can write a function that calls the procedure and then invoke that using SQL.
e.g.
create or replace function callmyproc
return sys_refcursor
IS
rc sys_refcursor;
BEGIN
myproc(rc);
return rc;
END;
Which you can then call with:
select callmyproc()
from dual;
When this example is run, the SQL Developer data grid shows one result but if you scroll right and click on the edit button, you will see the results in a grid.

Resources