Solved

Trigger/Function creation in Oracle regarding

Posted on 2008-10-02
6
505 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
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.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Oracle 12c patching 1 59
Help writing a query 6 69
grouping logic 6 46
Update data using formula 22 12
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
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 shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

948 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now