Why the select statement is not showing me table while executed in oracle? - oracle

I am new to oracle.I already have a table tempash.
So,I created a procedure to see the data of this table.
So,I created procedure as:
create or replace procedure offc.temp_sel(data1 varchar2) is
var1 varchar2(4000);
BEGIN
var1:='select * from offc.temp'||data1;
EXECUTE IMMEDIATE var1;
end;
So,I executed the statement but,it is not showing me anything.
exec offc.temp_sel('ash');
There is no any compilation error in my procedure.But why the select statement is not showing me data of that procedure?

Try adding out parameter:
create or replace procedure offc.temp_sel(data1 varchar2,result out sys_refcursor)
is
BEGIN
open result for 'select * from offc.temp'||data1;
end;
SQL> var rc refcursor
SQL> execute offc.temp_sel('ash',:rc)
PL/SQL procedure successfully completed.
SQL> print rc

You need to keep the result of the SELECT statement into a variable. As you perform SELECT * ..., you should put the result into a RECORD type but, as the result set contains more than 1 row, your variable needs to be a table of records.
In order to not be prone to error, the tables of records needs to be exactly like your table source structure.
CREATE OR REPLACE PROCEDURE OFFC.TEMP_SEL(DATA1 VARCHAR2) IS
VAR1 VARCHAR2(4000);
TYPE T_RESULT IS TABLE OF offc.temp%ROWTYPE;
-- defined the new type based on the structure of table TEMP from schema OFFC
v_result t_result;
-- define a variable of that type.
BEGIN
var1:='select * from offc.temp'||data1;
EXECUTE IMMEDIATE VAR1 BULK COLLECT INTO V_RESULT;
-- collect he result into the new variable
FOR I IN 1 ..v_result.count
LOOP
dbms_output.put_line(v_result(i).<<column_name from temp offc.table>>);
end loop;
-- loop through the variable(table of records) and display its content.
-- you need to replace the << ... >> with the name of your column from source tabel that you want to display.
end;
To execute the procedure, you should use:
set serveroutput on;
execute temp_sel( 'ash');
Best,
Mikcutu

Related

Create record from refcursor

I would like to create record from refcursor. My code:
set serveroutput on
DECLARE
c_curs SYS_REFCURSOR;
v_id NUMBER;
BEGIN
pck_prov.get_value_type_list (1, c_curs); --> procedure called here
-- I guess this is the place where record can be created from cursor.
LOOP
FETCH c_curs
INTO v_id;--instead of fetching into variable I would like to fetch into row
EXIT WHEN c_curs%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(v_id);--if fetching is done into row, only selected columns can be printed, like myrow.id
END LOOP;
CLOSE c_curs;
END;
Please note: I know how to create record from cursor which is defined with select statement as it is described here. What I don't know is how to use same technique for refcursors.
EDIT:
Code from here is just what I need, but it throws error:
set serveroutput on
VAR c_curs refcursor;
EXECUTE pck_prov.get_value_type_list(1, :c_curs);
BEGIN
FOR record_test IN :c_curs LOOP
dbms_output.put_line(record_test.id);
END LOOP;
END;
Error: error PLS-00456: item 'SQLDEVBIND1Z_1' is not a cursor.
Just to clarify question:
In my database there is around 200 packages.
Every package has several stored procedures inside - and usually each procedure is combined with columns from different tables. That is why it would be the best to have some dynamically created cursor, so I can make simple select just like in the example I've posted.
From Oracle 12.1 onward, you could use the DBMS_SQL.return_result procedure. SQL Plus displays the contents of implicit statement results automatically. So, rather than defining explicit ref cursor out parameters, the RETURN_RESULT procedure in the DBMS_SQL package allows you to pass them out implicitly.
DECLARE
c_curs SYS_REFCURSOR;
v_id NUMBER;
BEGIN
pck_prov.get_value_type_list (1, c_curs);
DBMS_SQL.return_result(c_curs); --> Passes ref cursor output implicitly
END;
/
In fact, no need of this separate PL/SQL block, you could add the DBMS_SQL.return_result(c_curs) statement in your original pck_prov.get_value_type_list procedure itself.
Just define a PL/SQL RECORD type that matches the cursor and FETCH into it.
DECLARE
c_curs SYS_REFCURSOR;
TYPE rec_t IS RECORD ( object_name VARCHAR2(30), object_type VARCHAR2(30) );
v_rec rec_t;
BEGIN
OPEN c_curs FOR
SELECT object_name,
object_type
FROM dba_objects
WHERE object_name like 'DBA%TAB%';
LOOP
FETCH c_curs
INTO v_rec;
EXIT WHEN c_curs%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(v_rec.object_name || ' - ' || v_rec.object_type);
END LOOP;
CLOSE c_curs;
END;
DBA_ADVISOR_SQLA_TABLES - VIEW
DBA_ADVISOR_SQLA_TABVOL - VIEW
DBA_ADVISOR_SQLW_TABLES - VIEW
DBA_ADVISOR_SQLW_TABVOL - VIEW
DBA_ALL_TABLES - VIEW
etc...

PL/SQL does not print to console (in SQLDeveloper)

I am writing PL/SQL trigger (INSTEAD OF type). I am trying to print to console in trigger, but it is not visible.
Script looks like this:
SET SERVEROUTPUT ON;
CREATE OR REPLACE TRIGGER MYTRIGG
INSTEAD OF INSERT ON MYTABLE
FOR EACH ROW
DECLARE
--My varables...
BEGIN
--Some statements
DBMS_OUTPUT.PUT_LINE('Error! Deptno does not exist!');
--Rest of script
END;
I have been searching through the stack and nothing works.
I have SET SERVEROUTPUT ON and I also tried to open View -> Dbms Output
Script compiles and works as expected but does not print to console.
Your code is misleading.
You can't have a INSTEAD OF trigger on a table.
Here's some working code.
drop table mytable;
drop view mytable_view;
create table mytable (a integer);
create view mytable_view as select * from mytable;
SET SERVEROUTPUT ON;
CREATE OR REPLACE TRIGGER MYTRIGG
INSTEAD OF INSERT ON MYTABLE_view
FOR EACH ROW
DECLARE
x integer;
BEGIN
--Some statements
DBMS_OUTPUT.PUT_LINE('Error! Deptno does not exist!');
--Rest of script
END;
/
insert into mytable_view values (1);
commit;
And if I run that as a script in SQL Developer -

PL SQL Procedure from Trigger

I created procedure GETDEL that select name from students , but when i try to call this procedure in trigger it says i gave wrong types of argument to call
I have been trying to pass (records) or (records OUT SYS_REFCURSOR) as a argument but it dont works
CREATE OR REPLACE PROCEDURE GETDEL(records OUT SYS_REFCURSOR) AS
BEGIN
OPEN records FOR
SELECT name FROM students;
END GETDEL;
CREATE OR REPLACE TRIGGER After_delete_student
AFTER DELETE ON TABLE2
FOR EACH ROW
DECLARE
rec sys_refcursor;
BEGIN
GETDELCZL();
END;
Your procedure contains a single parameter and, even though it's an out parameter, the calling procedure is responsible the defining it.
create or replace
trigger after_delete_student
after delete
on table2
for each row
declare
result_rec sys_refcursor;
begin
getdel(result_rec);
end;

How to write an Oracle procedure with a select statement (Specifically on SQL Developer)?

I want to create a simple Oracle Stored procedure on SQL Developer that will return some records on a simple select query. I do not want to pass in any parameter, but I just want the Records to be returned back from the procedure into a result set -> a suitable variable.
I have been trying to use the following syntax :
create or replace PROCEDURE Getmarketdetails2(data OUT varchar2)
IS
BEGIN
SELECT *
into data
from dual;
END Getmarketdetails2;
But it gives me an error while I try to execute with the following exec statement -->
Declare a Varchar2;
exec Getmarketdetails2(a);
Error: PLS-00103: Encountered the symbol "end-of-file" when expecting "something else".
Cause: Usually a PL/SQL compilation error.
Appreciate if anyone can help me out of this long pending situation! I have tried enough to find a basic guide to create a simple Oracle stored procedure and execute it in SQL Developer, but none of them answer to the point!!
You want:
DECLARE
a VARCHAR2(4000); -- Give it a size
BEGIN -- Begin the anonymous PL/SQL block
Getmarketdetails2(a); -- Call the procedure
DBMS_OUTPUT.PUT_LINE( a ); -- Output the value
END; -- End the anonymous PL/SQL block
/ -- End the PL/SQL statement
or:
VARIABLE a VARCHAR2(4000); -- Create a bind variable
EXEC Getmarketdetails2(:a); -- Execute the procedure using the bind variable
PRINT a -- Print the bind variable
Assuming an up-to-date Oracle version, you can use dbms_sql.return_result()
create or replace PROCEDURE Getmarketdetails2
IS
c1 SYS_REFCURSOR;
BEGIN
OPEN c1 FOR
SELECT *
from dual;
DBMS_SQL.RETURN_RESULT(c1);
END Getmarketdetails2;
/
Then simply run
exec Getmarketdetails2
The only drawback is that SQL Developer only displays the result as text, not as a proper result grid.
This is how I return a cursor in Oracle
PROCEDURE GetAllData (P_CURSOR OUT SYS_REFCURSOR)
IS
BEGIN
OPEN P_CURSOR FOR
SELECT *
FROM TABLE ;
END GetAllData ;
Declare a Varchar2;
exec Getmarketdetails2(a);
Your procedure is ok;
Instead of above query, use below query to run sp:
Declare
a Varchar2(10);
Begin
Getmarketdetails2(a);
End;

To get data from Stored Procedure having without parameter

I have a stored procedure proc1 without parameters. I want to extract data from this stored procedure. How can I get that? Could you help me?
Stored procedure:
create procedure proc1
as
begin
select e_id, e_nm, e_sal
from emp
where e_id like 'e%';
end proc1;
You can do this in Oracle 12.1 or above:
create or replace procedure demo
as
rc sys_refcursor;
begin
open rc for select * from dual;
dbms_sql.return_result(rc);
end demo;
This requires an Oracle 12.1 or later client/driver to handle the implicit result set.
For more details, see Implicit Result Sets in the Oracle 12.1 New Features Guide, Tom Kyte's Blog, Oracle Base etc.
Here's one possible solution:
Declaration:
create procedure proc1 (emp_row IN OUT emp%rowtype)
as
begin
select * --e_id, e_nm, e_sal
into emp_row
from emp
where e_id like 'e%';
end proc1;
Use case:
DECLARE
l_emp_row emp%rowtype;
BEGIN
proc1(l_emp_row);
-- Here you can access every column of table "emp", like so:
-- dbms_output.put_line('e_id: ' || to_char(l_emp_row.e_id));
-- dbms_output.put_line('e_nm: ' || to_char(l_emp_row.e_nm));
-- dbms_output.put_line('e_sal: ' || to_char(l_emp_row.e_sal));
END;
Is there anything special that you need to get out of the Procedure?
Cheers
you create a view this query. (recomended)
Oracle procedure is not return any data. So, you do not see any result. Your result get buffer but not print screen. if You need a procedure, insert all data another table.
create procedure proc1
as
begin
insert into new_table select e_id, e_nm, e_sal from emp where e_id like 'e%';
end proc1;
Another way; You create a function. Because function outputs and inputs. This function;
for example Create an Oracle function that returns a table

Resources