Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9247111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:39:04+00:00 2026-06-18T09:39:04+00:00

i try to create a trigger to prevent the insertion of duplicate entries in

  • 0

i try to create a trigger to prevent the insertion of duplicate entries in SQL Developer (Oracle 11g XPRESS) but it ‘s not compiled correctly. Can you help me why because I can’t see any obvious error in syntax.

CREATE OR REPLACE TRIGGER trig1

BEFORE INSERT ON table1

BEGIN 
  DECLARE CURSOR C1
  IS
    SELECT value1,value2 FROM inserted;

  DECLARE value11 number;
  DECLARE value22 number;
  OPEN C1;
  FETCH NEXT FROM C1 INTO @value11, @value22;

  WHILE FETCH_STATUS = 0
  LOOP
    IF NOT EXISTS (SELECT * FROM table1 WHERE value1 = @value11 AND value2 = @value22)
    THEN
        INSERT INTO table1 (value1,value2)
        VALUES
        (@value11, @value22);
    ELSE
        ROLLBACK TRANSACTION
        --DELETE FROM table1 WHERE value1 = @value11 AND value2 = @value22
        PRINT 'Cannot add duplicate entry.'
    END IF;
    FETCH NEXT FROM C1 INTO @value11, @value22;
  END LOOP; 
  CLOSE C1;
END;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T09:39:05+00:00Added an answer on June 18, 2026 at 9:39 am

    Most of the problems with your trigger are down to you using the wrong syntax; this looks like MySQL not PL/SQL. I would recommend reading the documentation and looking at some examples before continuing.

    Having said all that; you’re going about this all the wrong way. In order to prevent the insertion of duplicates you have to create a unique constraint on your table. It is the only way to guarantee that you prevent them; trying to work around it in code is bound to fail at some point.

    You can create a unique constraint inline, or if your table already exists you could create a unique index:

    CREATE UNIQUE INDEX index_name
        ON table_name (column1, column2, ... column_n);
    

    or use an ALTER TABLE statement:

    ALTER TABLE table_name
      add CONSTRAINT constraint_name UNIQUE (column1, column2, ... column_n);
    

    If the set of columns you’re testing against are the primary key you can add a primary key constraint instead.

    In addition to enforcing integrity no matter what your users decide enabling a unique constraint enables you to simply insert data and catch the errors. There’s no need to query the table prior to insertion, which should speed up your application.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to Oracle and SQL and I'm trying to create a trigger where
I try create a map for open ~/.vimrc, but open the ~/.vimrc only when
I try to create styles with first-child and last-child items but I encountered a
i try to create a plpgsql trigger for postgresql 8.3 which automatically partitions a
I got the following trigger on my sql server 2008 database CREATE TRIGGER tr_check_stoelen
I use squirrel 3.2.0 When I try to replace this trigger: CREATE OR REPLACE
I try create trigger CREATE TRIGGER `aster_users2` after update ON `aster_users` FOR EACH ROW
I try to create following trigger: DELIMITER $$ CREATE trigger insert_processor BEFORE INSERT ON
When I try to create this trigger, I get this exception: The name SITE_ID
I use MS SQL 2008 and I want to create a trigger in a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.