Oracle apex set row create username and create datetime on submit - oracle

I have order table with these columns:
id, customerid, orderdate, createusername, createdatetime
createusername and createdatetime aren't displayed in edit form - I want to set their values on form submit.

Set their default values to
:APP_USER for createusername
SYSDATE for createdatetime

Usually this is done with a trigger on the table. That way you don't have to worry about this anywhere in your apex application. That trigger would be something like this:
create or replace trigger order_biu
before insert or update
on order
for each row
begin
:new.createdatetime := sysdate;
:new.createusername := nvl(sys_context('APEX$SESSION','APP_USER'),user);
end order_biu;
/
I assumed that the column order.createdatetime is of datatype DATE. For other datatypes, you'll need another default value.

Related

how to save a oracle form field values to a database table

BEGIN
INSERT INTO tbmt_brg_agency_letters
(ID,
MTBRG_CREATE_DAT,
MTBRG_ORNNN_ID,
MTBRG_RATING_AGENCY_TXT,
MTBRG_EXTRNL_LTR_RATING_TXT)
VALUES
( MTBLR_SEQ.NEXTVAL,
:VWMTBRG.CREATE_DAT,
:VWMTBRG.ORNNN_ID,
:VWMTBRG.RATING_AGENCY_TXT,
:VWMTBRG.EXTERNAL_LETTER_RATING_TXT);
COMMIT;
END;
when i tried to save form values to a table it not saving, I'm using WHEN-BUTTON-PRESSED trigger.
I'm trying to insert one table values to other table which are selected using a dropdowm in a form.

Disparity between populated id column and returned value from hibernate after save

A sequence is used to generate id for a customer table and then using trigger to populate the id column as a varchar value.
The trigger is
CREATE OR REPLACE TRIGGER USER_ID_TRIGGER
before insert on CUSTOMER
REFERENCING NEW AS New OLD AS Old
for each row
begin
-- :new.user_id := 'CUST' || :new.user_id ;
select 'CUST' || SEQ_CUSTOMER_ID.nextval into :new.user_id from dual;
end USER_ID_TRIGGER;
So, each insert in the column is like 'CUST1', 'CUST3', etc.
The trigger can't be changed.
I am using hibernate save(customer) method to save customer objects in the db. Problem is the return id value I am getting is (as expected) different from the one that is ultimately saved in the table.
For example, if the id populated in the column is 'CUST19', the hibernate code returns 18.
My question is, if the code returns a value of 210, will it be safe for me to assume that the populated value is CUST211?

convert oracle table column name to MM/DD/YYYY

I would like convert the column name to date.
for example the column name is today, i want to convert it dynamically to today's date like MM/DD/YYYY .
as of now the column name is "Today" i want it to be current date
You can't configure a column to change its name automagically. To reflect the current day or whatever else.
But, you can change the column name by using an alias when doing a query. In order to make the things the more transparent as possible, you might want to create a view. Here is an example:
-- Some table with a column named "TODAY"
CREATE TABLE T AS (SELECT LEVEL today FROM DUAL CONNECT BY LEVEL < 5);
-- Use PL/SQL to create a view on the given table
-- with a dynamic column name
DECLARE
today varchar(10) := TO_CHAR(SYSDATE,'DD/MM/YYYY');
query varchar(200) := 'CREATE OR REPLACE VIEW V'
|| ' AS SELECT today "' || today || '"'
|| ' FROM T';
BEGIN
execute immediate query;
END;
Then, to query the "table" with the right column name, you will simply need to query V instead of T:
SELECT * FROM V;
12/12/2014
1
2
3
4
If you recreate your view daily, say by calling the above PL/SQL code from a job, you will see each day a view with the current date as the column name. But, as the underlying table is left unchanged, you will still be able to query it using the canonical name today. Which is important for example if you need to perform join on that table.
That being said, I'm not sure I will push toward such a solution. Use at your own risks!
If you want the column name heading to appear as something different than what the column name is defined in the table, you simply use the as "DisplayColumnName" clause for that column:
select user_name, today as "12/12/2014" from some_table;
But you would need to programatically generate the SQL statement for that to work. What coding environment you are using would dictate how to dynamically create a select statement.

How to write an Oracle trigger based on Auto incremented value?

I have an Oracle Table with the following keys: ID, Name, DoB, Dept & FileNo. The ID field is the primary Key with an Auto Incremented value.
I wish to write a trigger, so that when a row is added with the Name, DoB & Dept , the FileNo field should get the value yyyy/xxxx where 'yyyy' is a predefined string & 'xxxx' is the value in ID field.
How do I do this?
If it will always be the ID with some prefix, then it probably shouldn't be a column. If that is the default, then a trigger that sets :new.fileno := 'string'||:new.id should suffice.
Oracle doesn't have auto increment, so you probably mean a sequence. If you have a trigger populating that, then this can go in the same trigger.
You need a sequence to implement an Auto Incremented value:
create sequence seq_file_id start with 1 increment by 1;
and a trigger on a table
CREATE TRIGGER file_trg
BEFORE insert
ON file_table
FOR EACH ROW
BEGIN
SELECT seq_file_id.NEXTVAL INTO :new.id FROM dual;
:NEW.fileno := 'yyyy' || :new.id;
END;
/

Create oracle trigger that will update a new column with the concatenated values of the updated columns

I have firstName, lastName and fullName columns in oracle. I want to be able to create a trigger that updates the fullName column with the firstName and lastName values when they are either updated or inserted. I would think this would be possible to do in oracle. Does anyone have any ideas. Thanks very much.
create or replace trigger pick_a_name
before insert or update
on mytable
for each row
begin
:new.full_name := :new.firstName ||' '|| :new.lastName;
end pick_a_name;

Resources