Solved

Trigger/Function creation in Oracle regarding

Posted on 2008-10-02
6
506 Views
Last Modified: 2013-12-18
COLUMN1        COLUMN2
8983888          2
8983888             1
8983888             5

Need to sort this first


8983888            1
8983888         2
8983888            5


then I need to sequence COLUMN2


8983888           1
8983888           2
8983888           3


How to do this using a trigger(if possible)?

I am asking about a trigger because they will be inserted(using SQL Loader) and as soon as they are inserted, the tables should be structured according to the above requirement.

If not possible how to do that using a function? ( or ) a sql query?

Thanks in anticipation.
0
Comment
Question by:srikanthradix
6 Comments
 
LVL 9

Expert Comment

by:jamesgu
ID: 22630777
so the sequence of a new record is always incremented by 1?

you may use a sequence

CREATE SEQUENCE your_sequence
INCREMENT BY 1
START WITH 1

CREATE OR REPLACE TRIGGER tri_your_table
BEFORE INSERT ON your_table
FOR EACH ROW
BEGIN
      SELECT your_sequence.NEXTVAL
      INTO :new.sequence_column
      FROM dual;
END;
0
 

Author Comment

by:srikanthradix
ID: 22631209
Suppose the data is like this:

COLUMN1        COLUMN2
8983888             2
8983888             1
8983888             5
8983889             4
8983889             1
8983889             5

The Result should be

COLUMN1        COLUMN2
8983888             1
8983888             2
8983888             3
8983889             1
8983889             2
8983889             3

Sorry, I am not being clear.
0
 
LVL 28

Accepted Solution

by:
Naveen Kumar earned 250 total points
ID: 22631411
first load the data and then also you can run update command to update it as per your needs :

can you try :

update your_table x
set ( col1, col2 ) =
(  
select y.col1, y.val  from  ( select col1, col2, row_number() over (partition by col1 order by col2 ) val from your_table ) y
where x.col1 = y.col1
and x.col2 = y.col2
  );


0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 27

Assisted Solution

by:sujith80
sujith80 earned 100 total points
ID: 22631417
A trigger will not be able to do that.
Use the following query.
select column1, row_number() over(partition by column1 order by column2) column2
from <your table>;

Open in new window

0
 
LVL 15

Assisted Solution

by:Shaju Kumbalath
Shaju Kumbalath earned 150 total points
ID: 22633048
Follow the steps and try loading
1. Create a table to hold the seq

CREATE TABLE SRL.SEQ_PRM
(
PARAM_NAME VARCHAR2(30 BYTE),
COLUMN_ID NUMBER(8)
);
2. Create a procedure to populate this
 
CREATE OR REPLACE PROCEDURE p_getnewid (
param_nm IN VARCHAR2,
new_id OUT NUMBER
)
IS
begin
BEGIN
SELECT (NVL (column_id, 0) + 1)
INTO new_id
FROM seq_prm
WHERE UPPER (param_name) = UPPER (param_nm)
FOR UPDATE OF column_id;
EXCEPTION
WHEN NO_DATA_FOUND
THEN
INSERT INTO SEQ_PRM (PARAM_NAME)
VALUES (PARAM_NM);
new_id := 1;
END;
UPDATE seq_prm
SET column_id = new_id
WHERE UPPER (param_name) = UPPER (param_nm);

END;
/
 
3.  Create trigger on ur original table

create or replace trigger x BEFORE insert on table_x
for each row
begin

P_GETNEWID(:NEW.col1,:NEW.col2);

end;
 
Now try to load data
0
 

Author Closing Comment

by:srikanthradix
ID: 31502567
Thanks for solution.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that useā€¦
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question