Trigger to keep a single record form a table to a certain status
Hi all.
I have a table which has a "Status" field. I'm trying to accomplish, with a trigger, that the table can only have one record with an "Active" status, so whenever and Insert or Update occurs, if the new value for the "Status" field is "Active", I should update all other records in the table to "Inactive". I've tried several approaches, and managed to make it work on Inserts, but any Updates wll give an error that says that the table is mutating. I've tried to do it on before and after trigger types, without success.
Is there a way to do what I want with a trigger, or do necessarily I have to create an SP that needs to be called before the Insert or Update operation?
Alternatively you can try with as follows:
SQL> create table t as select rownum id, 'Inactive' status from dual connect by level <= 4
2 union all select 5, 'Active' from dual
3 /
Table created.
SQL>
SQL> select * from t
2 /
ID STATUS
1 Inactive
2 Inactive
3 Inactive
4 Inactive
5 Active
SQL> create or replace trigger t_trg
2 before insert or update
3 on t
4 for each row
5 begin
6 if (sys_context (
7 'userenv',
8 'client_info'
9 ) != 'start transaction'
10 or sys_context (
11 'userenv',
12 'client_info'
13 ) is null)
14 then
15 dbms_application_info.set_client_info ('start transaction');
16
17 execute immediate 'update t@loopback set status = ''Inactive'' where status = ''Act
ive'' and id != :id'
18 using :new.id;
19
20 dbms_application_info.set_client_info ('end transaction');
21 end if;
22 end t_trg;
23 /
Trigger created.
SQL>
SQL> update t
2 set status = 'Active'
3 where id = 2
4 /
1 row updated.
SQL>
SQL> select * from t
2 /
ID STATUS
1 Inactive
2 Active
3 Inactive
4 Inactive
5 Inactive
SQL>
SQL> update t
2 set status = 'Active'
3 where id = 4
4 /
1 row updated.
SQL>
SQL> select * from t
2 /
ID STATUS
1 Inactive
2 Inactive
3 Inactive
4 Active
5 Inactive
SQL>
SQL> insert into t values (6, 'Active')
2 /
1 row created.
SQL>
SQL> select * from t
2 /
ID STATUS
1 Inactive
2 Inactive
3 Inactive
4 Inactive
5 Inactive
6 Active
6 rows selected.
SQL>All you need is the loopback trigger wich I defined as
CREATE DATABASE LINK LOOPBACK
USING '(description=(address=(protocol=beq)(program=/path_to_your_oracle_binary/oracle)))';
Similar Messages
-
Open single record form for particular record
It's simple to open tabular form for particular record
- just add page item, pass value to this item
and add this item to WHERE clause.
But what is common approach for single record form?
Where is it described?here is my solution:
1) create page item which will receive primary key value
2) look at Sample Application Page 7 - customers edit page -
you need "Automated Row Fetch", called in "After Header" block -
Single Record Form: fields display with the wrong width
I'm laying out fields on a single record form and notice that the width of the fields displayed when I run the form is less than the width I specify on the "Edit Page Item" page. Here are 3 examples:
field 1: Display as=text, Width=100, Maximum Width=100, ==> displayed width=76
field 2: Display as=text, Width=50, Maximum Width=50, ==> displayed width=46
field 3: Display as=textarea Width=100 Maximum Width=100 ==> displayed width=100
Anyone know why this is happening? I'm trying to layout the form so that it has a pleasing look. It's hard to get the fields lined up when they don't display according to the widths specified.
Edited by: Corvette Captain on Aug 9, 2012 3:21 PMHi,
>
I'm laying out fields on a single record form and notice that the width of the fields displayed when I run the form is less than the width I specify on the "Edit Page Item" page. Here are 3 examples:
field 1: Display as=text, Width=100, Maximum Width=100, ==> displayed width=76
field 2: Display as=text, Width=50, Maximum Width=50, ==> displayed width=46
field 3: Display as=textarea Width=100 Maximum Width=100 ==> displayed width=100
Anyone know why this is happening? I'm trying to layout the form so that it has a pleasing look. It's hard to get the fields lined up when they don't display according to the widths specified.
>
Setting the Width of the Page Item sets the input elements "size" attribute. HTML rendering based on size attribute is not a precise as you need. You should use CSS for precision. See Compatibility Notes
Cheers, -
Any clue how to add pagination to single record form?!
Is there any1 who tried to create pagination to single record form?! i tried this but i have problem with navigating to the NEXT record.
Regards;Hello guys?!!
Is there any1 in this forum can help me in my issue?!! am desperate. -
Time out while reading single record from CRMD_ORDERADM_H table on OBJECTID
Hi,
This is the problem i am facing in CRMD_ORDERADM_H.
if i search for a single record in CRMD_ORDERADM_H Table using SE11 on OBJECT_ID field, it is giving me Time out error.
CRMD_ORDERADM_H db size would be > 1 Billion records.
It is having a Secondary Standard index on OBJECT_ID.
If i search for single record with OBJECT_ID and PROCESS_TYPE i am able to get the result within seconds.
But if i take some range in OBJECT_ID and single value in PROCESS_TYPE then i am getting Time out erro.
we have index (custom) on OBJECT_ID and PROCESS_TYPE combination.
What would be the cause?
Thanks in Advance,
-KishoreHello,
there is a special table for reading records from orders: CRMD_ORDER_INDEX.
Regards, R -
How to make few columns of single record inactive in Table conrol?
Hi all,
Scenario is like this : i have one table control , it is displaying 10 records , in that first four columns are editable. Now my requirement is, whenever record having intial values( suppose one record having initial values among ten) except key field , we need to deactive that particular record , now table control should contain ten records , but nine records are in editable state and one record in non editable state .
How to make few columns of single record inactive in Table conrol?
Thanks in advance.hi there...
select the column that u want to convert to non editable mode in the screen layout. then double click on it. go to the attributes tab and uncheck the input chk box. the check box will become disabled for input. u can do this for any number of columns.
if u want to do it dyanamically, loop at screen. check for the screen element's name. when itmatches the column u want to disable, simply set the input value as 0.
dont forget to modify sscreen after setting the attribute values.
i hope this helps.
do reward if helpful. -
Hi all,
I hava a table and when I create a new record I have to read the previous one for some check.
Which is the script to read a particular record in a table?
THANKS
ENZOThis is one of the many ways :
var dataline = xfa.form.data.PAGE.SUBFORM.TABLENAME.resolveNodes("DataRow[*]");
This puts all the nodes of a table into the dataline variable.
You can then use this code to loop through the table and access the field TEXTFIELD in that row :
for ( i = 0; i < dataline.length; i++ )
dataline.item(i).resolveNode("TEXTFIELD").rawValue;
You can use the resolveNode function to capture specific fields in a structure as well, using dots :
dataline.item(i).resolveNode("SUBTABLE.ROW2.TEXTFIELD").rawValue;
If you want to catch the last line, use :
dataline.item(dataline.length).resolveNode("TEXTFIELD").rawValue;
If you want to catch the second to last line, which I presume is what you want use :
dataline.item(dataline.length - 1).resolveNode("TEXTFIELD").rawValue; -
HI Everybody,
Could anyone be so kind to assist me in the following problem? I have built a master-detail form with several records displayed. Screen items are Read/Write, Read-only and text for domains in popup-lists (these will be populated at the beginning using a record group query). Now, when I have executed a query and there are some records fetched, I can click in any record below, thereby leaving empty records in between. This should not be allowed, because Forms--allegedly--always lets you have a single last record empty (for input), but only one!
Thank you in advance, kind regards from
Miklos HERBOLYRaj,
The multi-record forms on tables currently are not supported by Portal. However, there is a creative workaround of using MD forms with one hidden field has been described in this topic:
http://technet.oracle.com:89/ubb/Forum81/HTML/000757.html
Thanks,
Dmitry -
Insert Records into another tables
Hi guyz,
I have four tables as below
1) employee
empid (primary key)
deptid
emp_name
work_location
date_of_join
emp_status
date
2) Termination
empid reference employee
deptid
emp_name
work_location
terminate_date
3) Resignation
empid reference employee
deptid
emp_name
work_location
resign_date
4) Vacation
empid reference employee
deptid
emp_name
work_location
vac_date
i have list item on employee form --> emp_status is list item whic contain Termination, Resignation, Vacation
i want to update employee as well other tables if user select the respected field from the list item.
if the user select termination/resignation from the list item the date item will be enable and user should enter the date,otherwise give the error on commit please enter the date and when user commit the record on employee it should update the table employee and delete the record belongs to Termination/Reisgnation and the record will be insert into Termination/Resignation table as select by user from list item.i tried this before but im fail i write the below code on pre-update trigger as below
begin
insert into termination
(empid,deptid,emp_name,work_location,termination_date)select (empid,deptid,emp_name,work_location,date from employee where emp_status='Termination';
delete from employee where emp_status='Termination' ;
end;
same as on resignation
anybuddy help me how to do this task? to update the tables. im new in oracle.
Regards
Edited by: user10648713 on Dec 21, 2008 4:29 AMYou run into the classic Mutating table problem. You cannot issue DML-statement against the same table the trigger is based on in a FOR EACH ROW-trigger.
But even worse, you will cause any kind of client program to get in trouble if you implement such a logic. The client issues an update and as a result the record gets deleted. And, for you have a forms-module based i tell you that you will get problems with your forms-module.
I still can't see whats the sense behind it, putting records into different tables dependant on a status. Oracle is built to manage large data volumes, so even for a large number of records that doesn't make sense to me.
But anyway if you still want to deal with that multi-table-approach, go for the following:
You will need three triggers on your table: a before-statement, an after-statement and a for-each-row-trigger:
The before-statement will initialize a list of PK-Items
The for-each-row will add the id of the processed row to the list
The after-statement will loop over the list and process the rows.
Create a Package for handling the logic including three procedures
CREATE OR REPLACE PACKAGE PK_TRIGGER IS
PROCEDURE PR_BS;
PROCEDURE PR_ARIU(i_nEmpId IN NUMBER);
PROCEDURE PR_AS;
END;
CREATE OR REPLACE PACKAGE BODY IS
TYPE tData IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
lData tData;
PROCEDURE PR_BS IS
BEGIN
lData.DELETE;
END;
PROCEDURE PR_ARIU(i_nEmpId IN NUMBER) IS
iPos PLS_INTEGER:=lData.COUNT+1;
BEGIN
lData(iPos):=i_nEmpId;
END;
PROCEDURE PR_AS IS
CURSOR crEmp(i_nEmpId IN NUMBER) IS
SELECT *
FROM EMP
WHERE EMP_ID=i_nEmpId;
recEmp crEmp%ROWTYPE;
iPos PLS_INTEGER;
BEGIN
iPos:=lData.FIRST;
LOOP
EXIT WHEN iPos IS NULL;
-- Do whatever you want with the data in the record
OPEN crEmp(lData(iPos));
FETCH crEmp INTO recEmp;
IF crEmp%FOUND THEN
IF :NEW.EMP_STATUS='Termination' THEN
DELETE FROM emp
WHERE EMPID=lData(iPos);
INSERT INTO TERMINATION (
empid,
deptid,
emp_name,
work_location,
t_date
) VALUES (
recEmp.empid,
recEmp.deptid,
recEmp.emp_name,
recEmp.work_location,
sysdate
END IF;
END IF;
CLOSE crEmp;
-- Process next record
iPos:=lIds.NEXT(iPos);
END LOOP;
lIds.DELETE;
END;
END;Now create the three triggers calling the appropiate procedures. -
Select record from database table
how to select a record from data-base table for current month in module-pool, i.e if current month is august then records selected should between 1/08/10 to 31/08/10.
And, i also want to select records for last month i.e if current month is august then records selected should be between 1/07/10 to 31/07/10.
And one thing more, it should work for every scenario. Like for months are changing then it should work according to that.
Example: If month changes to October then current month ll be October and last month ll become September.
Plz help me out.
Moderator message: you chose a better forum this time, but the question is still too basic to be asked here, please (re)search yourself first, these forums are not catering to beginners.
locked by: Thomas Zloch on Aug 8, 2010 9:41 PMHi Nishant,
You can use the option <b>ORDER BY </b> in your SELECT clause.
This is the SAP Documentation for <b>ORDER BY </b> option.
<i>Variant 1</i>
<b>...ORDER BY PRIMARY KEY</b>
<b>Effect</b>
Sorts the selected lines in ascending order by the primary key of the database table. This variant is only permitted for SELECT * ....
<i>Notes</i>
Since views do not have a primary key, specifying ORDER BY PRIMARY KEY only makes sense with database tables. If, however, you do specify ORDER BY PRIMARY KEY with a view, all fields of the view are sorted in ascending order.
<i>Variant 2</i>
<b>ORDER BY f1 ... fn</b>
<i>Effect</i>
Sorts the selected records in ascending order by the specified column references f1 ... fn. If a list is also specified in the SELECT clause, the column references f1, ..., fn must appear in this list.
By supplementing the statement with DESCENDING, you can sort in descending order using any of the fields f1, ..., fn.
The default sort sequence is ascending order, but you can make this explicit by adding the addition ASCENDING.
Consider this code this will select the latest 10 records form DB table(Note can use <b>Variant 2</b> for DB tables also.
REPORT zarun_1.
DATA : it_mara TYPE STANDARD TABLE OF mara WITH HEADER LINE.
START-OF-SELECTION.
SELECT * FROM mara
INTO TABLE it_mara
<b>UP TO 10 ROWS "No of rows you need(give 1 here)
ORDER BY matnr DESCENDING. "Specify the Key Fields here.</b>
LOOP AT it_mara.
WRITE : / it_mara-matnr,
it_mara-mtart.
ENDLOOP.
Regards,
Arun Sambargi.
Message was edited by: Arun Sambargi -
Copy records of one table into another using Update statement
Supposing I have 2 tables test and test1.
Test has columns col1 and col2. and test1 has only one table col1.
select * from test returns:
COL1 COL2
a
b
c
d
e
select * from test1 returns
COL1
p
q
r
s
t
Suppose i want to copy values of test1.col1 to test.col2 so tht final result of
select * from test should be
COL1 COL2
a p
b q
c r
d s
e t
I found a query in the OCP introduction book:
update test set col2=(select col1 from test11)
this works fine only when we have a single record in test1 table but fails otherwise.
how can this be achieved using the update statement ?SQL> desc test
Name Null? Type
COL1 VARCHAR2(10)
COL2 VARCHAR2(30)
SQL> desc test1
Name Null? Type
COL1 VARCHAR2(10)
SQL> insert into test values ('a','');
1 row created.
SQL> insert into test values ('b','');
1 row created.
SQL> insert into test values ('c','');
1 row created.
SQL> insert into test values ('d','');
1 row created.
SQL> insert into test1 values ('e');
1 row created.
SQL> insert into test1 values ('f');
1 row created.
SQL> insert into test1 values ('g');
1 row created.
SQL> insert into test1 values ('h');
1 row created.
SQL> commit;
Commit complete.
SQL> ed
Wrote file afiedt.buf
1 select a.col1, b.col1, a.rn from
2 (select row_number() over (order by col1) rn, col1 from test) a,
3 (select row_number() over (order by col1) rn, col1 from test1) b
4* where a.rn = b.rn
SQL> /
COL1 COL1 RN
a e 1
b f 2
c g 3
d h 4
SQL> ed
Wrote file afiedt.buf
1 update test set col1 =
2 (
3 select forupd from (
4 select a.col1, b.col1 test1col, a.col1||' '||b.col1 forupd, a.rn from
5 (select row_number() over (order by col1) rn, col1 from test) a,
6 (select row_number() over (order by col1) rn, col1 from test1) b
7 where a.rn = b.rn
8* ) z where z.col1 = test.col1)
SQL> /
4 rows updated.
SQL> commit;
Commit complete.
SQL> select * from test;
COL1 COL2
a e
b f
c g
d h
SQL>
This will work only if you have the same number of lines in COL1 in both tables. If you have different number of lines, then you need to code more at join (outer, inner).
But for complicated cases, please post sample data.
Michael
PS: And supossing that the values are distinct in TEST table. Else update will fail, because more rows will be retrived.
If more values in TEST table in COL1, then you need to assign a row number on TEST also, and in WHERE of query you need to place one more join condition:
something like:
8* ) z where z.col1 = test.col1
and z.rn = test.rn)
SQL> /
But as i said, get more specifications.
Message was edited by:
kjt
Added the PS. -
Can I use a trigger to keep record of deleted data?
I am trying to log changes to a table, including deletes. The trigger is working effectively for INSERT and UPDATE, but does not capture the data from the row when I create a DELETE trigger. I have tried various trigger syntax methods to capture the data in a new "log" table, but I only seem to get the key field after the trigger fires.
My base table has only a few fields: ID (key), NAME, DESCRIPTION, CREATED_ON, UPDATED_ON.
On delete, I would like to capture the data that was deleted for record keeping and audit trails. Below is the latest iteration of my trigger define code:
create trigger ADDRESS_TYPES_TRIGGER_D
BEFORE DELETE ON "MEDPORTAL"."MEDPORTAL_XS.data::ADDRESS_TYPES"
REFERENCING OLD ROW AS thisRow
FOR EACH ROW
begin
declare newId INT;
declare deleteId INT := 131;
select "MEDPORTAL"."MEDPORTAL_XS.data::Address_Types_Log_ID".NEXTVAL into newID from DUMMY;
INSERT INTO "MEDPORTAL"."MEDPORTAL_XS.data::ADDRESS_TYPES_LOG"
(ID,
USERNAME,
ACTION_ID,
ADDRESS_TYPE_ID,
NAME,
DESCRIPTION,
UPDATED_ON
values(
:newId,
current_user,
:deleteId,
:THISROW.ID,
:THISROW.NAME,
:THISROW.DESCRIPTION,
current_timestamp
end
This code captures the newId from the sequence as the new table key. It also captures the USERNAME, ACTION_ID (coded to a delete message in another table), ADDRESS_TYPE_ID (THISROW.ID), and the current_timestamp. How can I capture the data in NAME and DESCRIPTION in the log table?
I have tried
create trigger ADDRESS_TYPES_TRIGGER_D
AFTER DELETE ON "MEDPORTAL"."MEDPORTAL_XS.data::ADDRESS_TYPES"
REFERENCING OLD ROW AS thisRow
FOR EACH ROW
and
create trigger ADDRESS_TYPES_TRIGGER_D
AFTER DELETE ON "MEDPORTAL"."MEDPORTAL_XS.data::ADDRESS_TYPES"
FOR EACH ROW
But I cannot capture the data. I am on the AWS rev 70 image (1.0.70.386119)
Is the DELETE trigger capable of capturing the data?If you can add a step to disable backup job start of ETL and enable backup job after ETL completes.
exec msdb..sp_update_job @job_name = 'LS backup Job Name', @enabled = 0 --Disable
exec msdb..sp_update_job @job_name = 'LS backup Job Name', @enabled = 1 --Enable
create a record in a table:
In those line you can create a new job as well which will check record in table if its 0(ETL completed) then enable the backup job and if its 1(ETL running/started) then disable the job. This job will run every 5 mins to check the flag in table. Depending
on ETL status it will enable or disable tlog backup job that way you will be able to resume log shipping without delay. -
Reverse auto-population to a shell attribute form/ single record instance BP.
Hi All,
I was wondering if anyone had experience in reverse auto-populating data from a BP within a shell to that same shell's attribute form or a single record instance BP like Project Information.
My use case is that I have 7-10 BP's that gain more definition and have data within in them that changes from phase to phase. This leaves a need for one place with the most current information, which I thought Shell Attributes or Project Information.
That being said as the information changes in theses 7-10 BP's throughout the life of the project the Shell Attribute form and Project Information should be updated via Reverse auto-population.
Can anyone tell me if this scenario is possible, or if there is another way that i can achieve this?
Your help would be appreciated!!!Yes, it is possible and fairly simple. Make sure that the DEs from the various BPs that will be passed are on the form of the shell or single instance record. And make sure that integration is defined for those DEs on the shell or SI record. This is a common mistake, as people forget to define the integration and that is a must for reverse auto-pop.
Then, on the BPs passing the data, open the form from which they will be passed and look at the properties. Define the reverse auto-pop normally, SI records are always available for this, and shells should be too.
Sean -
All,
I have an application which has a form where users submit accounts for review. It is working great and populates a table in my schema, but of course we need to have these accounts reviewed by my team. I am attempting to design a button that will pull a single random account that was submitted into a report/form (not sure which to use) so that my team can review them for errors and have an 'accept' or 'deny' button.
What would be the best method for designing such a review. The button would need to execute PL/SQL that would update the 'user id' column of the person clicking the button and thus "assigning the account to them" and display that single record in this report/form so that they can easily review it for approval. I have already written the SQL that will pull a random record from the table. The Accept or Reject buttons would insert a Y or N into the accepted column.
We are using Oracle 11g and Apex 3.2. I am just not the best at PL/SQL and not sure how to get such a button to launch this form.
On a separate note I have used JQuery and have seen the posts on how to create a form via JQuery but not sure how to bring in a random loan with by executing SQL.Any detail form can be made into this "random form" . Say you have detailed form based on the submitted records table. Now it would have a hidden field holding the PK value. If you were to link to this page from a any other page setting this primary key field, it would automatically populate the form with the record details.
So if the button that "randomly" pulls out a record were to redirect to this page and set the PK field with some value, it would be fetched and can be processed.
Suppose you don't set the PK field, but rather add an "onload before-header" PLSQL process in the same that fetches a random PK value(among submitted records) and set the PK feild automatically. Any redirection to the form would then pick up and show a random record.
That process could be as simple as
BEGIN
SELECT <PK column> INTO :<page PK Item>
FROM ( SELECT <PK column>
FROM <table>
WHERE <filter critera>
ORDER BY dbms_random.value
WHERE rownum=1;
END;So just a create a normal detailed edit form and add this process to the onload(make sure it runs first).
Change the button to redirect to this page. -
Want to show data from current month & prev month in single record on forms?
Hello experts,
i have this requirement:
Table structure:
MOnth Division totalAmount
01-apr-2013 1 10000
01-May-2013 1 20000
01-apr-2013 2 30000
01-May-2013 2 50000
i want to display in forms like
Form:
Month : 01-May-2013
Month Division total prev_Month division total dIFFERENCE
01-May-2013 1 10000 01-Apr-2013 1 20000 10000
01-May-2013 2 30000 01-Apr-2013 2 50000 20000
I am using two procedure for fetching records for current month and previous month.
but it will cause problem, if for some division previous month data not present.
please suggest me some query with which i can fetch current and pre month data in single record.
Please help me out.
Thanks
yashI'M NOT SURE BUT TRY IT ............
select x.mn mnth, x.division, x.totalAmount total, y.mn prev_month, y.division, y.totalAmount total, (y.totalAmount - nvl(y.totalAmount,0)) difference
from
(select month mn, division, totalAmount
from table_name
where month = (select max(month)
from table_name
) x,
(select month mn, division, totalAmount
from table_name
where month = (select add_months(max(month),-1)
from table_name
) y
where x.division = y.division(+)
MOSTAFIZ MITUL
DHAKA BANGLADESH
Maybe you are looking for
-
Is there a way to create dependency on the real-time jobs
Hi, We have around 80 real-time services running and loading the changed data into the target. The process being used is IBM Informix > IBM CDC > JMS (xml messages) > DS real-time services > Oracle EDW. While using the above process, when ever there
-
Error while creating the repair order
I am getting an error when the repair order automatically creates the services it giving the error...."No entry for plant 1200, service object 01 CSREPAIR in table T399A" So please can any one tell me that why its performing like that? Thanks Mohsin
-
Using DBMS_ALERT package
Hello, I would like to use DBMS_ALERT package. I have read documentation about this package. It works correctly but I have noticed that sometime the pl/sql block (with dbms_alert) performed quick but sometimes it lasted too long. There is no traffic
-
Weird downsampling, pixelated, images in Safari & Firefox
Since applying the latest update (to Snow Leopard), images on websites look highly pixelated (often background images). After applying the recent update to iPhone - it does the same thing! Perhaps something in the latest webkit release? My wife's iPh
-
Some data missing on iOS 5.0.1
Hi, would anyone help me? I had problems on reading Presentations (.ppt) and PDFs. As an engineer and lecturer, it is impossible to use iPad (2) for educational purposes because I make presentations using Equation Editor and when read on iPad (2) Wi-