#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    2
    Rep Power
    0

    Removing Duplicates


    I'm applying a sequence to a list of attributes sequentially, and if you have already incremented the sequence (made a primary key) for a attribute and then you come across the same attribute in the list, how do you stop it from incrementing the sequence because I have already created primary key for it.

    For example takes the series of insert statements below:

    INSERT INTO REC_COMPANY VALUES(company_seq.nextval,'Columbia');
    INSERT INTO REC_COMPANY VALUES
    (company_seq.nextval,'CBS Records');
    INSERT INTO REC_COMPANY VALUES(company_seq.nextval,'RCA');
    INSERT INTO REC_COMPANY VALUES(company_seq.nextval,'Virgin records');
    INSERT INTO REC_COMPANY VALUES(company_seq.nextval,'BMG');
    INSERT INTO REC_COMPANY VALUES(company_seq.nextval,'Polydor');
    INSERT INTO REC_COMPANY VALUES(company_seq.nextval,'CBS');
    INSERT INTO REC_COMPANY VALUES(company_seq.nextval,'Pickwick');
    INSERT INTO REC_COMPANY VALUES(company_seq.nextval,'Polydor');
    INSERT INTO REC_COMPANY VALUES(company_seq.nextval,'Atlantic');
    INSERT INTO REC_COMPANY VALUES(company_seq.nextval,'Mega');
    INSERT INTO REC_COMPANY VALUES(company_seq.nextval,'Grammy');
    INSERT INTO REC_COMPANY VALUES(company_seq.nextval,'Musik Master');
    INSERT INTO REC_COMPANY VALUES(company_seq.nextval,'Columbia');
    INSERT INTO REC_COMPANY VALUES(company_seq.nextval,'Polydor');
    INSERT INTO REC_COMPANY VALUES(company_seq.nextval,'WEA');
    INSERT INTO REC_COMPANY VALUES(company_seq.nextval,'London');
    INSERT INTO REC_COMPANY VALUES
    (company_seq.nextval,'EMI');
    INSERT INTO REC_COMPANY VALUES(company_seq.nextval,'Columbia');

    In this example the record company 'columbia' comes up three times and therefore has three primary keys as i have to increment the sequence counter with every insert statement.

    Can i use "if" and "else" to say that if the value exists then don't create a new sequence. How do i do this ?

    I need some PL/SQL code to say something like

    if the value in column two already exists
    then do nothing
    else
    insert into table
    end if


    Any ideas on how to right such code? Any help would be much appreciated!
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2002
    Posts
    56
    Rep Power
    13
    You could use a function for this but why not place a unique index on name?


    INSERT rec_company (NAME) values ('EMI') would fail if EMI already exists.
    Casuistry Agnostic

IMN logo majestic logo threadwatch logo seochat tools logo