PLSQL Get All table Data - oracle

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');

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

Function works in PL/SQL but not in PowerBi

When I call the function, it works in SQL*Plus but doesn't work in PowerBI.
I opened PowerBI> Get Data> Oracle> Entered server name> Went to advanced options to enter sql below
SELECT * FROM TABLE(TESTPOWERBI);
Error: We encountered an error while trying to connect. SQL command not properly ended.
Anyone have experience in solving this?
DROP TYPE VW_PEOPLE_TABLE;
DROP TYPE VW_PEOPLE_TYPE;
CREATE OR REPLACE TYPE VW_PEOPLE_TYPE AS OBJECT(NAME VARCHAR2(70), ALIAS VARCHAR2(90));
/
CREATE OR REPLACE TYPE VW_PEOPLE_TABLE AS TABLE OF VW_PEOPLE_TYPEL
/
CREATE OR REPLACE 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 MYDATABASE;
FOR i in 1 .. VWT.COUNT
LOOP
PIPE ROW (VW_PEOPLE_TYPE(VWT(i).NAME, VWT(i).ALIAS));
END LOOP;
END TESTPOWERBI;
/
GRANT EXECUTE ON TESTPOWERBI TO PUBLIC;
You mixed your delimiters ; and /.
Clean it up and add one at the end behind GRANT EXECUTE ON TESTPOWERBI TO PUBLIC.
In addition you got a typo: for i in 1 .. vwt.count only two dots.
This should work:
DROP TYPE VW_PEOPLE_TABLE;
DROP TYPE VW_PEOPLE_TYPE;
CREATE OR REPLACE TYPE VW_PEOPLE_TYPE AS OBJECT
(
NAME VARCHAR2 (70),
ALIAS VARCHAR2 (90)
);
CREATE OR REPLACE TYPE VW_PEOPLE_TABLE AS TABLE OF VW_PEOPLE_TYPEL;
CREATE OR REPLACE 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 MYDATABASE;
FOR i IN 1 .. VWT.COUNT
LOOP
PIPE ROW (VW_PEOPLE_TYPE (VWT (i).NAME, VWT (i).ALIAS));
END LOOP;
END TESTPOWERBI;
/
GRANT EXECUTE ON TESTPOWERBI TO PUBLIC;
Here some discussion about delimiters:
When do I need to use a semicolon vs a slash in Oracle SQL?

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

Return table from function in Oracle

Following function works with SQL Server.
CREATE FUNCTION GET_EMP_PROB_START_ATT_CODE(#EVAL_TYPE VARCHAR(6))
RETURNS #EMP_PROB_START_ATT_CODE TABLE (
PROBATION_START_ATT_CODE varchar(6) NULL
)
AS
BEGIN
IF #EVAL_TYPE='New'
INSERT INTO #EMP_PROB_START_ATT_CODE (PROBATION_START_ATT_CODE)
SELECT NEW_EMP_PROB_START_ATT_CODE FROM HS_HR_PEA_DYNAMIC_ATTRIBUTES;
ELSE
INSERT INTO #EMP_PROB_START_ATT_CODE (PROBATION_START_ATT_CODE)
SELECT EXIS_EMP_PROB_START_ATT_CODE FROM HS_HR_PEA_DYNAMIC_ATTRIBUTES;
RETURN;
END;
I just want to convert this to Oracle equivalent query . Tried but not succeeded.
CREATE FUNCTION GET_EMP_PROB_START_ATT_CODE(EVAL_TYPE VARCHAR(6))
RETURN EMP_PROB_START_ATT_CODE IS TABLE
PROBATION_START_ATT_CODE VARCHAR(6) NULL;
AS
BEGIN
IF EVAL_TYPE='New'
INSERT INTO EMP_PROB_START_ATT_CODE (PROBATION_START_ATT_CODE)
SELECT NEW_EMP_PROB_START_ATT_CODE FROM HS_HR_PEA_DYNAMIC_ATTRIBUTES;
ELSE
INSERT INTO EMP_PROB_START_ATT_CODE (PROBATION_START_ATT_CODE)
SELECT EXIS_EMP_PROB_START_ATT_CODE FROM HS_HR_PEA_DYNAMIC_ATTRIBUTES;
RETURN;
END;
Warning: compiled but with compilation errors
Any help guys..
First of all, create a database-wide type:
create type string_tab6 is table of varchar2(6);
Then you would create your function like so:
create or replace function get_emp_prob_start_att_code(eval_type varchar2)
return string_tab6
is
v_array string_tab6;
begin
if eval_type = 'New' then
select new_emp_prob_start_att_code
bulk collect into v_array
from hs_hr_pea_dynamic_attributes;
else
select exis_emp_prob_start_att_code
bulk collect into v_array
from hs_hr_pea_dynamic_attributes;
end if;
return v_array;
end get_emp_prob_start_att_code;
/
However, I would question why you need such a function. What are you going to use it for?
In Oracle, I'd expect to see that logic embedded directly into whichever SQL statement needs that info, which you could easily do via a case statement.
First define row type. For existing database table you can use %ROWTYPE.
Then define table type.
Finally use the table type as return type of the function.
TYPE tr_row IS my_table%ROWTYPE;
TYPE tab_row IS TABLE OF tr_row;
FUNCTION get_row RETURN tab_row IS
mytab tab_row;
BEGIN
SELECT * FROM my_table
BULK COLLECT INTO mytab;
RETURN mytab;
END;

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;
/

Resources