To get data from Stored Procedure having without parameter - oracle

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

Related

Why the select statement is not showing me table while executed in 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

PLSQL Get All table Data

I have a function in PLSQL and trying to call it from PowerBI which I finally got it to work.
Currently to return a tables columns, I need to manually specify the types. Is there a way to simply edit this code to return all columns of any given table?
Meaning that given this script, I only need to change the name of the table in the script and it should give me all the data.
I have to use PLSQL for this as there are some Auth protection to query tables which I cant simply query from PowerBI.
Drop Type VW_PEOPLE_TABLE;
Drop Type VW_PEOPLE_TYPE;
Drop Function TESTPOWERBI;
Drop Public Synonym PBI;
CREATE TYPE VW_PEOPLE_TYPE AS Object
{
Name VarChar2(70).
ALIAS VarChar2(90)
};
/
Create Type VW_PEOPLE_TABLE as TABLE OF VW_PEOPLE_TYPE;
/
Grant execute on VW_PEOPLE_TABLE TO public;
Create Function TESTPOWERBI
Return VW_PEOPLE_TABLE Pipelined AUTHID current_user AS VWT VW_PEOPLE_TABLE;
Pragma AUTONOMOUS_TRANSACTION;
Begin
Select VW_PEOPLE_TYPE(NAME,ALIAS)
Bulk COLLECT INTO VWT
FROM mytable;
FOR I IN 1 .. VWT.count
LOOP
PIPE ROW(VW_PEOPLE_TYPE(VWT(I).NAME, VWT(I).ALIAS));
END LOOP;
END TESTPOWERBI;
/
create public synonym PBI for TESTPOWERBI;
Grant Execute on PBI to public;
EDIT : as per this powerBI community post :
You should be able to achieve it using a procedure like this.
CREATE OR REPLACE PROCEDURE get_query_result (
p_tab_name VARCHAR2,
p_rc OUT SYS_REFCURSOR
) AS
BEGIN
OPEN p_rc FOR 'select * FROM '
|| p_tab_name;
END;
/
In Oracle environment, If you are using Oracle 12c and above, you may use DBMS_SQL.RETURN_RESULT with a dynamic REFCURSOR.
CREATE OR REPLACE PROCEDURE get_query_result (
p_tab_name VARCHAR2
) AS
rc SYS_REFCURSOR;
BEGIN
OPEN rc FOR 'select * FROM '
|| p_tab_name;
dbms_sql.return_result(rc);
END;
/
Get the query result for any table using
EXEC get_query_result('EMPLOYEES');

FORALL+ EXECUTE IMMEDIATE + INSERT Into tbl SELECT

I have got stuck in below and getting syntax error - Please help.
Basically I am using a collection to store few department ids and then would like to use these department ids as a filter condition while inserting data into emp table in FORALL statement.
Below is sample code:
while compiling this code i am getting error, my requirement is to use INSERT INTO table select * from table and cannot avoid it so please suggest.
create or replace Procedure abc(dblink VARCHAR2)
CURSOR dept_id is select dept_ids from dept;
TYPE nt_dept_detail IS TABLE OF VARCHAR2(25);
l_dept_array nt_dept_detail;
Begin
OPEN dept_id;
FETCH dept_id BULK COLLECT INTO l_dept_array;
IF l_dept_array.COUNT() > 0 THEN
FORALL i IN 1..l_dept_array.COUNT SAVE EXCEPTIONS
EXECUTE IMMEDIATE 'INSERT INTO stg_emp SELECT
Dept,''DEPT_10'' FROM dept_emp'||dblink||' WHERE
dept_id = '||l_dept_array(i)||'';
COMMIT;
END IF;
CLOSE dept_id;
end abc;
Why are you bothering to use cursors, arrays etc in the first place? Why can't you just do a simple insert as select?
Problems with your procedure as listed above:
You don't declare procedures like Procedure abc () - for a standalone procedure, you would do create or replace procedure abc as, or in a package: procedure abc is
You reference a variable called "dblink" that isn't declared anywhere.
You didn't put end abc; at the end of your procedure (I hope that was just a mis-c&p?)
You're effectively doing a simple insert as select, but you're way over-complicating it, plus you're making your code less performant.
You've not listed the column names that you're trying to insert into; if stg_emp has more than two columns or ends up having columns added, your code is going to fail.
Assuming your dblink name isn't known until runtime, then here's something that would do what you're after:
create Procedure abc (dblink in varchar2)
is
begin
execute immediate 'insert into stg_emp select dept, ''DEPT_10'' from dept_emp#'||dblink||
' where dept_id in (select dept_ids from dept)';
commit;
end abc;
/
If, however, you do know the dblink name, then you'd just get rid of the execute immediate and do:
create Procedure abc (dblink in varchar2)
is
begin
insert into stg_emp -- best to list the column names you're inserting into here
select dept, 'DEPT_10'
from dept_emp#dblink
where dept_id in (select dept_ids from dept);
commit;
end abc;
/
There appears te be a lot wrong with this code.
1) why the execute immediate? Is there any explicit requirement for that? No, than don't use it
2) where is the dblink variable declared?
3) as Boneist already stated, why not a simple subselect in the insert statement?
INSERT INTO stg_emp SELECT
Dept,'DEPT_10' FROM dept_emp#dblink WHERE
dept_id in (select dept_ids from dept );
For one, it would make the code actually readable ;)

Oracle stored procedure to return a list of ids

I'm trying to convert an sql stored procedure to oracle. It's a very trivial procedure. It just returns a list of ids from a table, which is very easy to do in sql.
SQL
CREATE PROCEDURE [dbo].[tspInstalledLanguages]
AS
BEGIN
SELECT language_id from languages
END
I'm a complete novice when it comes to oracle, and I've learned that it's not as straight forward as this.
I've tried the following:
Oracle
CREATE OR REPLACE PROCEDURE tspInstalledLanguages
AS
BEGIN
SELECT LANGUAGE_ID FROM LANGUAGES;
END;
With no luck.
I get a message that it's expecting select into
It is.
Example:
DECLARE
v_authName author.author_last_name%type;
BEGIN
SELECT author_last_name
INTO v_authName
FROM author
WHERE author_key = 'A103';
dbms_output.put_line('Name: '||v_authName);
END;
/
So using your code:
CREATE OR REPLACE PROCEDURE tspInstalledLanguages
IS
v_language NUMBER;
BEGIN
SELECT LANGUAGE_ID
INTO v_language
FROM LANGUAGES;
dbms_output.put_line(v_language);
END;
/

how to use date in where clause while inserting values into table using PLSQL store procedure

This is the code i used in stored procedure;
CREATE OR REPLACE PROCEDURE MY_STORE_PROCEDURE (new_date in date)
IS
BEGIN
execute immediate 'INSERT INTO TEMP_1 ( ID CHAR(10),
A_CNT NUMBER,
JOIN_DT DATE,
)
SELECT
L1.ID,
L1.A_CNT,
L1.JOIN_DT,
FROM ACTVY_1 L1
WHERE L1.JOIN_DT = new_date';
END;
===========================================================
Below is the code i used to call store procedure with passing value. value is date which store procedure reciece and used to pull date from a table. but it is giving me error.
DECLARE
a_date DATE;
BEGIN
a_date :=to_DATE ('01-NOV-2013', 'DD-MON-YYYY');
MY_STORE_PROCEDURE(a_date);
END;
Please suggest is there any syntax error or what is issue.
Based on your example, there is no reason to use dynamic SQL. You also have a bunch of errors. Try this:
CREATE OR REPLACE PROCEDURE MY_STORE_PROCEDURE (new_date IN DATE)
IS
BEGIN
INSERT INTO TEMP_1 (ID, A_CNT, JOIN_DT)
SELECT L1.ID, L1.A_CNT, L1.JOIN_DT
FROM ACTVY_1 L1
WHERE L1.JOIN_DT = new_date;
END;

Resources