Solved

Trigger/Function creation in Oracle regarding

Posted on 2008-10-02
6
508 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

738 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