Oracle "DBMS_METADATA.GET_DDL" using question - oracle

Using DBMS_METADATA.GET_DDL question
DBMS_METADATA.GET_DDL('PROCEDURE', 'PCD_TBL_BLOB_INSERT', 'TEST') was used to output the procedure and function DDL
When using double quotes when creating for the first time
CREATE PROCEDURE "TEST"."PCD_TBL_BLOB_INSERT"
ex.SELECT DBMS_METADATA.GET_DDL('PROCEDURE', 'PCD_TBL_BLOB_INSERT', 'TEST') FROM DUAL;
CREATE OR REPLACE FUNCTION "TEST"."PCD_TBL_BLOB_INSERT"
~~
CREATE PROCEDURE "TEST"."RESULT1"
BEGIN
NULL;
END
SELECT DBMS_METADATA.GET_DDL('PROCEDURE','RESULT1','TEST') FROM DUAL;
RESULT1.
CREATE OR REPLACE NONEDITIONABLE PROCEDURE "TEST"."RESULT1"
BEGIN
NULL;
END
2.When first created
CREATE PROCEDURE PCD_TBL_BLOB_INSERT
SELECT DBMS_METADATA.GET_DDL('PROCEDURE', 'PCD_TBL_BLOB_INSERT', 'TEST') FROM DUAL;
ex. CREATE OR REPLACE FUNCTION "TEST"."PCD_TBL_BLOB_INSERT" PCD_TBL_BLOB_INSERT
~~
CREATE PROCEDURE RESULT2
BEGIN
NULL;
END
SELECT DBMS_METADATA.GET_DDL('PROCEDURE','RESULT2','TEST') FROM DUAL;
RESULT2.
CREATE OR REPLACE NONEDITIONABLE PROCEDURE "TEST"."RESULT2" RESULT2
BEGIN
NULL;
END
Even if you create it like result 2, I want result 1
oracle version : Oracle Database 12c Enterprise Edition Release 12.2.0.1.0

Related

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

Procedure conversion in Oracle

I am a new member here so apologies from me if i am not able to follow the rules here.
I am using Oracle 11g express edition along with pl/sql. I am trying to convert a database of sql server in oracle 11g. So far i have done everything.
But now i am trying to convert procedures. I have done all changing but still i am not able to make procedures completely.
This is code of procedure in SQL server:
create proc Result_proc
#name varchar(40),
#Test varchar(40)
as
(
select * from [Result View] where ([Student Name] = #name AND Test# IN (#Test))
)
Converted code in ORACLE:
create or replace procedure Result_proc (
p_name varchar2,
p_Test varchar2)
as
begin
(
select * from ahmad where (Student_Name = p_name AND Test# IN (p_Test))
)
end;
Here in the image you see when i run the converted code in PL/SQL it is not executed properly:Output of CONVERTED proc in PL/SQL
Output:
SQL> 10
In SQL Server this stored procedure means 'return result of this select statement to the client'. It's common way in MSSQL to return recordsets.
When using Oracle, default way of do this is to use ref cursor output parameter. This procedure should be converted in something similar to
create procedure Result_Proc(p_Name varchar2, p_Test varchar2, p_Result out sys_refcursor) is
begin
open p_result for
select * from "Result View" where student_name = p_Name and test# = p_Test;
end;
But generally better way is to drop such junk procedures at all. Unlike MSSQL, Oracle does not require such select SPs as security call. More powerful and handy method is to just select what you need directly.

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

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

Run Oracle Procedure just to lock row, without returning a resultset

I want to run a procedure to force a row lock on a row, but I don't want to return a result set to the client, nor do I actually want to update anything. Below is the proc:
CREATE OR REPLACE PROCEDURE SP_LOCK_Row
(IDRow IN INTEGER)
IS
BEGIN
SELECT *
FROM TBLTable
WHERE IDRow = IDRow
FOR UPDATE;
END;
The problem is that I keep getting the error: PLS-00428: an INTO clause is expected in this SELECT statement. Is there a way for me to lock the row without actually having to return a result set back to the client? The SQL Server equivalent is:
CREATE PROCEDURE dbo.SP_LOCK_Row(
#IDRow INT)
AS
SELECT *
FROM dbo.TBLTable WITH (UPDLOCK, ROWLOCK)
WHERE IDRow = #IDRow
Tks
I don't know how smart it is to code this way but this:
CREATE OR REPLACE PROCEDURE SP_LOCK_Row
(IDRow IN INTEGER)
IS
dummy varchar2(1);
BEGIN
SELECT 'x' into dummy
FROM TBLTable
WHERE IDRow = IDRow
FOR UPDATE;
END;
should do the trick.
Ronald - http://ronr.blogspot.com

Resources