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)
Related
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
I made a procedure called 'VENTAS_MAYOR' with a parameter 'FECHITA' type DATE
CREATE OR REPLACE PROCEDURE VENTAS_MAYOR (FECHITA IN DATE)
IS
V_FECHA DATE;
V_CANTIDAD NUMBER;
V_DESCRIPCION VARCHAR2(50);
BEGIN
SELECT
A.FECHAEMISION_BOL,
B.CANTIDAD,
C.DESCRIPCION
INTO
V_FECHA,
V_CANTIDAD,
V_DESCRIPCION
FROM BOLETA A JOIN DETALLE B ON (A.COD_BOLETA = B.COD_DETALLE)
JOIN PRODUCTO C ON (B.COD_DETALLE = C.CODPRODUCTO)
WHERE A.FECHAEMISION_BOL = FECHITA
ORDER BY CANTIDAD DESC;
DBMS_OUTPUT.PUT_LINE(V_CANTIDAD || V_DESCRIPCION);
END VENTAS_MAYOR;
But when i am going to execute the function/procedure with a parameter i get that error...
SET SERVEROUTPUT ON;
EXECUTE VENTAS_MAYOR(05/2019);
I don't know why i'm getting this error , i'm using only a parameter.... !
You are passing the string as a parameter but oracle is expecting a date.
You need to pass a date as follows:
VENTAS_MAYOR(date'2019-05-01');
Since you need to pass input in date format. Correct way of calling this procedure is,
SET SERVEROUTPUT ON;
EXECUTE VENTAS_MAYOR(to_date('05/01/2019','mm/dd/yyyy');
Also, make sure datatype of FECHAEMISION_BOL column in BOLETA table. It should be DATE or TIMESTAMP. If not, you need to modify the WHERE condition as follows -
to_date(A.FECHAEMISION_BOL,'mm/dd/yyyy') = FECHITA
// Below update statement is present in my Stored procedure. I am passing two parameters (parameter 1 and parameter 2) while executing the Stored procedure. Once executing the Stored procedure i want the different updated values to be displayed. please provide the code for the below example(my stored procedure)
CREATE OR REPLACE PROCEDURE UPDATE_TABLE(parameter1 IN NUMBER, parameter IN varchar2)
AS
BEGIN
UPDATE Table1 SET column_a = (parameter1 +2) WHERE id= parameter2;
update Table2 set column_b= parameter1 where id=parameter2;
END UPDATE_TABLE
Use returning clause + bulk collect. I don't know for what purpose you need this updated data to be displayed. Below approach for one of your table. You can share it to others.
DECLARE
TYPE t_type is table of VARCHAR2(250);
l_type t_type;
begin
UPDATE Table1 SET column_a = (parameter1 +2) RETURNING column_a BULK COLLECT INTO l_type;
FOR i IN 1..l_type.count
LOOP
DBMS_OUTPUT.PUT_LINE(l_type(i));
END LOOP;
END;
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.
I'm trying to return a result set from a 11g Oracle table, after I've received the set back I need to set all the fetched rows as updated.
The below script returns only one row at a time, and I can't really get the update to work as I like. Some basic explanation would be highly appriciated.
Maybe it's also a bad idea using the rowtype?
CREATE OR REPLACE PROCEDURE OWNER_EXT.X_GETEXTERNALACCOUNTREF(result out X_externalaccountref%rowtype)
IS
cur SYS_REFCURSOR;
BEGIN
open cur for select * from X_externalaccountref WHERE externalstatus IS NULL;
LOOP
FETCH cur INTO result;
update X_externalaccountref set externalstatus = 1, externalchangedate = SYSDATE() where X_id = result.X_id;
EXIT WHEN cur%NOTFOUND;
END LOOP;
close cur;
END;
/
Why you only get one record returned:
You loop over the recordset, fetching one record at a time, and each time you assign this record to the output variable. So only your last record will be stored in the output variable.
What you want to do is return a record set. For this, you can use pl/sql tables. To easily fetch a set into a table you could use bulk collect into (a link - but google will find you a lot more). You can use the rowtype as the type for your table, or a type you make up yourself. You could use a package variable to declare a type, or define a type in the database schema. If you use rowtype, that is not bad at all. If your table should change, then rowtype will reflect this aswell, saving you the hassle of trawling through your code to add or remove columns should you have manually declared each column in a own type.
Ex:
function hand_out_money
return pck_emp.t_tab_emp
is
tab_emp pck_emp.t_tab_emp;
begin
select *
bulk collect into tab_emp
from emp
where deptno = 20;
update emp
set comm = comm + 10
where deptno = 20;
return tab_emp;
end;
To be able to use this, have t_tab_emp declared in a package_spec. This way, you can reference the returned type from where you call this code. Just put type t_tab_emp is table of emp%rowtype; in there - like i would do in package pck_emp.
Your calling code could then be:
declare
tab_result pck.t_tab_emp;
begin
tab_result := hand_out_money;
end;
To do your update:
If you fetch all your record into a plsql table, you could thereafter do a single update statement:
update X_externalaccountref
set externalstatus = 1, externalchangedate = SYSDATE
where externalstatus IS NULL;
Also: you could just use a function if you only return this one variable, it would make sense for a getter.
Also: i generally don't like function or procedures named 'getxxx' who then do DML. More proper would be to call your procedure 'activate_external_accounts' for example, which would then return the recordset. Mind, that if you do a bulk select before the update, the externalstatus and externalchangedate won't be updated! So you don't get a resultset returned, but a pre-update set.
update X_externalaccountref
set externalstatus = 1
, externalchangedate = SYSDATE
where externalstatus is null;
Why not?