views:

69

answers:

3

Can someone explain how to see the results of a procedure, everything is working fine and the code is valid, executed and compiled with no errors. Now how can I see the results as Query or anything.

The ex procedure is about sum of salary.

CREATE OR REPLACE PROCEDURE HR.TOTAL_SALARY AS    
   total_salary NUMBER(12,2);    
BEGIN    

  SET TRANSACTION READ ONLY;    

  SELECT SUM (salary) 
    INTO total_salary 
    FROM employees;    

  DBMS_OUTPUT.PUT_LINE('Total salary 1: ' || total_salary);     
  COMMIT;    

END; 
A: 

To output the results of a select statement in a procedure you need to use a cursor.

create procedure myproc 
(in_variable IN number, out_records OUT sys_refcursor)
as
begin
open out_records for

select * from mytable
where column = in_variable;

end;

then to use it, declare the cursor, execute the proc, and output the results.

variable records refcursor;
exec myproc(1, :records);
print :records;

(no promises that the above is syntactically perfect - I'm away from the DB right now. But it should be close enough to get you in the right direction.)

Oh - and you can use a user-defined cursor type inside of a package, if that is appropriate for your environment.

sql_mommy
Thank you!, I will try to analyse it and try to make it work...
Elisabeth_c21
+2  A: 

Are you running this in SQL*Plus? Have you "set serveroutput on;"?

jonearles
it doesnt work it says error "SET"..
Elisabeth_c21
You mentioned "SQL Worksheet" above. Does this mean you're using Oracle SQL Developer? If so, click on the DBMS_OUTPUT tab, click the Enable DBMS Output button, and run the procedure again. Make sure you don't have other worksheets open with DBMS Output enabled or they can steal each others output.
jonearles
No, im using Oracle 11g Enterprise Manager, but managed to apply dbms_output.enable (1000000); and trying to access it in "SQL Worksheet" by by entering SELECT * FROM HR.TOTAL_SALARY WHERE NAME = 'TOTAL_SALARY' ORDER BY LINE; But I get error: ora-04044 procedure function package or type is not allowed here
Elisabeth_c21
+1  A: 

I recommend for this a function

CREATE OR REPLACE FUNCTION HR.TOTAL_SALARY return number AS    
   total_salary NUMBER(12,2);    
BEGIN    

  SELECT SUM (salary) 
    INTO total_salary 
    FROM employees;    

return total_salary;

END; 

The usage for this is like:

select hr.TOTAL_SALARY() as total_sal from dual.
Florin Ghita