Simple Select inside an Oracle Stored Procedure - oracle

how do you create a stored procedure with a simple select (SELECT * FROM TABLE) using Oracle? Also, any good tutorials on stored procedures would help tremendously.
Thanks.

It depends on what you're trying to return from the stored procedure (resultset vs. scalar value) and what version of Oracle you're on (newer versions make this easier).
This question is probably a dupe of Get resultset from oracle stored procedure.

create or replace procedure spr_select_Emp(eno in number, employee out emp%RowType)
As
Begin
Select empno,ename,mgrno,hiredate,sal,comm,deptno into employee from emp
where empno=eno
End;

A procedure is created using the Oracle create or replace procedure syntax below:
create or replace procedure
()
as (or is)
local variable declaration
begin
code section
exceptions
end;
more info here: http://www.dba-oracle.com/t_create_or_replace_procedure.htm

Related

How to call Oracle stored procedure from azure data factory v2

My requirement is copy data from Oracle to SQL Server. Before copying from Oracle database, I need to update the Oracle table using procedure which has some logic.
How do I execute Oracle stored procedure from Azure datafactory?
I referred to this thread
if I use EXECUTE PROC_NAME (PARAM); in preCopy script it's failing with following error
Failure happened on 'Source' side.
ErrorCode=UserErrorOdbcOperationFailed,
Type=Microsoft.DataTransfer.Common.Shared.HybridDeliveryException
Message=ERROR [42000] [Microsoft][ODBC Oracle Wire Protocol driver]
[Oracle]ORA-00900: invalid SQL statement
Source=Microsoft.DataTransfer.ClientLibrary.Odbc.OdbcConnector,
Type=System.Data.Odbc.OdbcException
Message=ERROR [42000] [Microsoft][ODBC Oracle Wire Protocol driver]
[Oracle]ORA-00900: invalid SQL statement,Source=msora28.dll
Could anyone help on this?
Note: I am using self-hosted runtime environment for data factory
thanks!!
I used a Lookup Activity and a SELECT statement of DUAL TABLE. Due to the stored procedures can not be call from a statement SELECT. I created an oracle function and the function calls the stored procedure. The function returns a value and this value is received by the lookup activity.
When you define the function, you have to add the statement PRAGMA AUTONOMOUS_TRANSACTION. This is because Oracle does not allow to execute DML instructions with a SELECT statement by default. Then, you need to define that DML instructions in the Stored Procedure will be an autonomous transaction.
--Tabla
CREATE TABLE empleados(
emp_id NUMBER(9),
nombre VARCHAR2(100),
CONSTRAINT empleados_pk PRIMARY KEY(emp_id),
);
create or replace procedure insert_empleado (numero in NUMBER, nombre in VARCHAR2) is
begin
INSERT INTO empleados (emp_id, nombre)
Values(numero, nombre);
COMMIT;
end;
create or replace function funcinsert_empleado (numero in NUMBER, nombre in VARCHAR2)
return VARCHAR2
is
PRAGMA AUTONOMOUS_TRANSACTION;
begin
insert_empleado (numero, nombre);
return 'done';
end;
--statement in query of lookup
SELECT funcinsert_empleado ('1', 'Roger Federer')
FROM DUAL;
Example lookup
This is example in Spanish. https://dev.to/maritzag/ejecutar-un-stored-procedure-de-oracle-desde-data-factory-2jcp
In Oracle, EXECUTE X(Y) is a SQL*Plus-specific command shortcut for the PL/SQL statement BEGIN X(Y); END;. Since you are not using SQL*Plus, try the BEGIN/END syntax.
In case you only want to execute the DML query using the Azure Data Factory without procedure on oracle database :-
I have another solution where you can use the copy activity with the pre-copy feature of sink in-spite of lookup activity.
For this approach just follow the below steps :-
Keep both the source table and sink table as same ( Let say table A ) using the same linked service.
In sink use the pre-copy script feature and keep the DML (Insert/Update/Delete ) query that you want to perform over the table B.( This table is not necessary to be same as table A )
In case you want to avoid the copy of data to same table you can select query option in the source part and provide a where clause which is not going to satisfy and hence no copy of data will happen .
or you can create a table temp with one column and one row .
I have tested both the options and it works ... good part of above solution is you can avoid the procedure or function creation and maintenance .

Create temporary tables in Oracle stored procedure to show in Crystal Reports

In SQL Server I can create stored procedures which creates a temp table, insert values into it, and then return a select from that temp table to be the result set for a composite Crystal Report.
I have no idea how to perform it in Oracle stored procedures.
I know I can create a string variable and then execute immediate. But then I don't know how to insert values, and that the result set will be the Crystal Report source.
You may try it using plsql procedure as follows.
CREATE PROCEDURE testRS (lcout OUT sys_refcursor) AS
BEGIN
OPEN lcout
FOR
SELECT object_name, object_type
FROM user_objects;
END testRS;
sys_refcursor is a weak cursor, meaning it can point to any query, and no type is enforced.
To execute under sqlplus (similar API should be available under crystal report), you will need to define a sqlplus variable, which holds resultset from cursor inside the procedure.
-- Define sqlplus variable
SQL> var ncc refcursor;
-- Call to procedure.
SQL> exec TESTPKG.testRS( :ncc );
PL/SQL procedure successfully completed.
-- Display the resultset.
SQL> print :ncc;
Hope it helps,
Dhimant

Select All records from a table using SP in Oracle SQL Developer

I'm using SQL Oracle to build a stored procedure. I'm trying to build a stored procedure of the SQL-query below.And I want to return those data to a C# program.
select * from employee_master
I have tried following. Is this correct?
CREATE OR REPLACE PROCEDURE EMPLOYEE_SELECTALL (p_recordset OUTSYS_REFCURSOR)AS
BEGIN
OPEN p_recordset FOR
SELECT
*
FROM
EMPLOYEE_MASTER;
END EMPLOYEE_SELECTALL;
If you wish to build a stored procedure that return such resultset first of all you should check if you really need to do this. It's incidental and not recommended way for Oracle. But if you really need so, you should use REF CURSOR.
after executing your stored procedure in SQL Developer, it automatically brings back any output for you to view, including one or more ref cursors.
Example code and screenshots here

Procedure created with compilation errors in oracle.?

I wrote simple stored procedure in oracle. but its shows procedure created with compilation errors.
My code is:
CREATE OR REPLACE PROCEDURE PRC_SELECT
AS
BEGIN
SELECT * FROM tb_name;
END;
/
Please help me to solve this problem.
This is a very basic skeleton, for your requirement.
CREATE OR REPLACE PROCEDURE PRC_SELECT(p_OLD IN VARCHAR2)
AS
my_rec tb_name%rowtype;
BEGIN
SELECT * into my_rec FROM tb_name WHERE old = p_OLD;
END;
/
my_rec will be created as a PL/SQL type whose structure would be table tb_name's structure!
a procedure cannot do a select * back to the screen.
you would have to bulk collect it into a collection and return it or open a cursor and return that.
a procedure is pl/sql, which doesn't directly write to the screen.
if you want it, you can use dbms_output.put_line to write each record.
you might also want to look into pipelined functions, which can be used in SQL later.
it depends what you're requirements are really.
If you really want to read all the rows in tb_name you could try:
CREATE OR REPLACE PROCEDURE PRC_SELECT
AS
BEGIN
FOR aRow IN (SELECT * FROM tb_name)
LOOP
DBMS_OUTPUT.PUT_LINE('Retrieved ' || aRow.SOME_FIELD);
END LOOP;
END;
Note that this may produce a lot of output if there are many rows in tb_name.
Share and enjoy.

In PL/SQL, can I pass the table schema of a cursor FROM clause via a stored procedure parameter?

In PL/SQL, I would like to pass in a "source" schema as a parameter to a stored procedure. For instance:
BEGIN
CURSOR my_cursor IS
SELECT my_field FROM <schema>.my_table
...
I want the 'schema' value to come from an input parameter into the stored procedure. Does anyone know how I could do that?
P.S. Sorry if this is a stupid simple question, but I'm new to PL/SQL and must get some functions written quickly.
In addition to what Mark Brady said, another dynamic SQL option is to use a REF CURSOR. Since your sample code includes a cursor this would be the most relevant.
PROCEDURE select_from_schema( the_schema VARCHAR2)
IS
TYPE my_cursor_type IS REF CURSOR;
my_cursor my_cursor_type;
BEGIN
OPEN my_cursor FOR 'SELECT my_field FROM '||the_schema||'.my_table';
-- Do your FETCHes just as with a normal cursor
CLOSE my_cursor;
END;
This has to be done with dynamic sql.
Either the DBMS_SQL package or the Execute Immediate statement.
You can't use variables in the FROM clause.
A potential solution may be to
ALTER SESSION SET Current_Schema = '' <-- the schema you want.
That command changes the default schema. SO if you have a bunch of identically named tables you can save yourself dynamic SQL and make a Dynamic Alter Session.

Resources