pl/sql stored update procedure in oracle apex - oracle

I observed some really weird update problem with my pl/sql stored procedure here's the code:
create or replace procedure "UPDATECURRENTOFFICE"
(trans_pk IN NUMBER,
office_pk IN NUMBER)
is
BEGIN
UPDATE TRANSACTION
SET TRANSACTION.OFFICE_PK_CURRENT = office_pk
WHERE TRANSACTION.TRANS_PK = trans_pk;
end;​
it ends up updating every record in the table instead of just only one record. im using this pl/sql procedure inside a process with a process point of submit - after computations and validation.. Im new with apex, and I find it really hard making online systems with it. please help me T.T

Change the parameter name.
The sql parser inside PL/SQL for the query
UPDATE TRANSACTION
SET TRANSACTION.OFFICE_PK_CURRENT = office_pk
WHERE TRANSACTION.TRANS_PK = trans_pk;
recognizes trans_pk as the column trans_pk not the pl/sql variable. So the WHERE condition is read as WHERE TRANS_PK=TRANS_PK witch means WHERE TRANS_PK IS NOT NULL
The following should work
create or replace procedure "UPDATECURRENTOFFICE"
(inp_trans_pk IN NUMBER,
office_pk IN NUMBER)
is
BEGIN
UPDATE TRANSACTION
SET TRANSACTION.OFFICE_PK_CURRENT = office_pk
WHERE TRANSACTION.TRANS_PK = inp_trans_pk;
end;​

The problem here is that you have a column in your table and a parameter to your stored procedure both called TRANS_PK. You may have written your parameter trans_pk, but in Oracle, identifiers (such as parameter names) are case-insensitive.
It happens that in your query, the name trans_pk is recognised as a column name rather than a parameter name. So your query will update every row where the TRANS_PK value equals the TRANS_PK value. This will be every row in the table, unless there are any rows where TRANS_PK is not NULL. However, given the name of this column I suspect it's part of the primary key of the TRANSACTION table and so won't contain any NULL values.
If you use a prefix for function/procedure parameter names, such as p_, this problem goes away. Here's a fixed version of your stored procedure:
create or replace procedure "UPDATECURRENTOFFICE"
(p_trans_pk IN NUMBER,
p_office_pk IN NUMBER)
is
BEGIN
UPDATE TRANSACTION
SET TRANSACTION.OFFICE_PK_CURRENT = p_office_pk
WHERE TRANSACTION.TRANS_PK = p_trans_pk;
end;​

You could rename the parameter, but best practice is to always use aliases when using SQL in PL/SQL, e.g.:
create or replace procedure "UPDATECURRENTOFFICE"
(trans_pk IN NUMBER,
office_pk IN NUMBER)
is
BEGIN
UPDATE TRANSACTION
SET TRANSACTION.OFFICE_PK_CURRENT = UPDATECURRENTOFFICE.office_pk
WHERE TRANSACTION.TRANS_PK = UPDATECURRENTOFFICE.trans_pk;
end;​
This code is resilient to unexpected changes to the table structure.

Related

Problem with translating mysql command to oracle command - triggers

I had trouble converting the following command to the oracle command.
I will be glad if you help!
Create Trigger sales_stock_reduction
On SalesMovements
After insert
as
Declare #ProductId int
Declare #Piece int
Select #ProductId=ProductId, #Piece=Piece from inserted
Update Uruns set stock=stock - #Piece where ProductId=#ProductId
In this code, when sales are made, the number of stocks in the product table is reduced through the sales movement table.
I could not write this code in oracle. Wonder how to write in Oracle
You can convert that like this
CREATE OR REPLACE TRIGGER sales_stock_reduction
AFTER INSERT ON SalesMovements
FOR EACH ROW
DECLARE
v_ProductId inserted.ProductId%type;
v_Piece inserted.Piece%type;
BEGIN
BEGIN
SELECT ProductId, Piece
INTO v_ProductId, v_Piece
FROM inserted;
EXCEPTION WHEN NO_DATA_FOUND THEN NULL;
END;
UPDATE Uruns
SET stock=stock - v_Piece
WHERE ProductId=v_ProductId;
END;
/
In Oracle :
OR REPLACE clause is used whenever the trigger needs to be edited
local variables might be defined as the data type of those have
within the table
each individual statements end with a semi-colon
exception handling for NO_DATA_FOUND is added due to presuming at most one
row returns from the current query for the inserted table which doesn't have
a WHERE condition to restrict the result set

Using DECODE in PL/SQL procedure call

I have a stored PL/SQL procedure (say X) that inserts records into a table. I am calling that procedure from another procedure (say Y). I have some parameters in procedure Y like para1,para2,para3 which can have two values either zero or one, for zero and one values I have one id stored in a TBL_SETUP, and when I call procedure X I want to check that if para1 is null then return null, if it is not null then check if it is one then return YES_ID and if it is no then return NO_ID.
I have tried something like this. I wrote a SELECT statement for getting YES_ID,NO_ID before calling the procedure and it is working fine, but when I write procedure call as below, it is giving me error "PLS-00204: Function or pseudo-column may be used inside a SQL statement only". How to use DECODE in a procedure call?
PROC_X(DECODE(para1,NULL,NULL,DECODE(para1,'1',YES_ID,NO_ID)),para2,NULL,NULL,DECODE(para2,'1',YES_ID,NO_ID)),para3,NULL,NULL,DECODE(para3,'1',YES_ID,NO_ID)),)
You could use SELECT INTO:
DECLARE
DECODE_RESULT VARCHAR2(100); -- or any suitable data type
BEGIN
SELECT DECODE(...) INTO DECODE_RESULT FROM dual;
PROC_X(DECODE_RESULT);
END;

Calling a Procedure in Select Statement

I have a package consists of function and procedures
CREATE OR REPLACE PACKAGE BODY schema.pkg_product as
function xxxxx()
procedure product_get(p_product_id IN Number,
P_direct_balance Out Number,
P_indirect_balance Out Number) IS
v_request CLOB :=<soapenv:Envelope xmnlns:----->
v request_end varchar(100) := <can:----->
BEGIN
Selct statement
END
My question is can we can Call the procedure in the select statement and how can I get the P_direct_balance , P_indirect_balance. if so in the select query coz I want to retrieve that data from the procedure.
It's not possible. You could write a function but you cannot use functions with out parameters in SQL. So you'll have to rewrite your code. For instance create two functions that return p_direct_balance and p_indirect_balance.
If rewriting the procedure to be a function with an out parameter is not possible, I would suggest altering the procedure to write the relevant values to a temporary table and commit. Then use your select statement to retrieve the values from the temp table.

"Procedure successfully completed", but does not show the output, even when it should return values

I have created a stored procedure in SQL Developer (Oracle):
CREATE OR REPLACE Procedure SP_test
(Identificadores IN varchar,
separador IN varchar,
codigo IN char,
resultSet out sys_refcursor)
AS
BEGIN
open resultSet for
SELECT DISTINCT
(tb.CODIGO||separador||tb.NOMBRE) AS Campo
FROM TABLA tb
WHERE
tb.CODIGO = codigo
and concat(tb.IDENTIFICADOR,tb.TECLA) in (Identificadores);
-- and concat(tb.IDENTIFICADOR,tb.TECLA) in ('FR45');
END;
/
The problem arises when executing the stored procedure, since the result is: "procedure successfully completed", but does not show any data. In contrast, if you only change the WHERE clause in the Identifiers variable by its value (hardcode the value, as in the commented line), it does return all the rows that correspond. I do not understand xq if you pass the same value as parameter does not work. So I execute the proce:
set serveroutput on;
variable rs refcursor;
exec SP_test('FR45','|', '1',:rs);
print rs;
There is no error with any of the other parameters, and although I have tried sending only "FR45" as parameter Identifiers, the application could send a list of codes separated by "," (from there importing of using IN and not a simple EQUAL) . I've already tried using "instr ()" instead of IN and even creating views ... and nothing.
It's more than 4 days and I can not find a solution: (please .. any idea what I should do so that the procedure recognizes the Identifiers parameter in the IN clause and returns the column of data that I need? :(
PS I had the same procedure In SQL-Server and it worked perfect..

Update statement inside oracle stored procedure is not working

I have one simple update statement inside oracle stored procedure. Its executing successfully but its not updating the table.
CREATE OR REPLACE PROCEDURE UpdateSourceLog
( SourceLogId IN NUMBER, TotalRowCount IN INT,Status IN VARCHAR)
AS
BEGIN
UPDATE SourceLog
SET Status = Status,
TotalRowCount = TotalRowCount,
EndTime = SYSDATE
WHERE SourceLogId = SourceLogId;
COMMIT;
END;
I have tried with changing the perameter name different from column name. Then also Its not working.
And I have tried with anonymous block. I'm not able to find out the isue. Please help me in this regard.
Thanks!
It is a bad practice to give parameters the same name as table columns.
So you should change it:
CREATE OR REPLACE PROCEDURE UpdateSourceLog
( p_SourceLogId IN NUMBER, p_TotalRowCount IN INT,p_status IN VARCHAR)
AS
BEGIN
UPDATE SourceLog
SET Status = p_status,
TotalRowCount = p_TotalRowCount,
EndTime = SYSDATE
WHERE SourceLogId = p_SourceLogId;
COMMIT;
END;
Because for now, most likely, Oracle understands it as column names and just update column to value from this column (no sense at all)

Resources