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 7724681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:45:00+00:00 2026-06-01T04:45:00+00:00

IF EXISTS (SELECT name FROM sysobjects WHERE name = ‘myTrigger’ AND type = ‘TR’)

  • 0
IF EXISTS (SELECT name FROM sysobjects WHERE name = 'myTrigger' AND type = 'TR')
BEGIN
    DROP TRIGGER myTrigger
END
GO
go
create trigger myTrigger
on mytable_backup
instead of insert
as
begin
  declare @seq int
  select @seq = seq from inserted 
  if exists (select * from mytable_backup where seq= @seq) begin
      delete from mytable_backup where seq=@seq
  end
  insert into mytable_backup
  select * from inserted
end
go

I’ve written this trigger to check while inserting if seq column is repeated then update the previous row with same seq if seq doesn’t exits insert it with new seq.

In ssis package I’m using OLEDB table(Mytable) as a source which contains.

Name,Age,Seq
Gauraw,30,1
Gauraw,31,1
Kiran,28,3
Kiran,29,3
kiran,28,3
Venkatesh,,4
Venkatesh,28,4

Now I’m loading this table to OLEDB destination(Mytable_backup) as destination.
I suppose to get output as.

Gauraw,31,1
kiran,28,3
Venkatesh,28,4

But I’m getting all the records from Mytable into Mytable_backup.

is anything wrong with my trigger?

  • 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-01T04:45:01+00:00Added an answer on June 1, 2026 at 4:45 am

    I think that this trigger will just take the first row and compare it with the existing. If I understand what you want to do you can quit easy do this:

    IF EXISTS (SELECT name FROM sysobjects WHERE name = 'myTrigger' AND type = 'TR')
    BEGIN
        DROP TRIGGER myTrigger
    END
    GO
    go
    create trigger myTrigger
    on mytable_backup
    instead of insert
    as
    begin
    insert into mytable_backup
    select 
        * 
    from 
        inserted
    WHERE NOT EXISTS
        (
            SELECT
                NULL
            FROM
                mytable_backup AS mytable
            WHERE
                inserted.seq=mytable.seq
        )
    end
    go
    

    EDIT

    So I found out what was going on. If you insert all of the rows in one go the inserted contains all the rows.. Sorry my mistake. If there are duplicates in your data your example do not show which to choose. I have chosen the one with the maximum of age (don’t know what your requirements is). Here is a update with the full example

    Table structure

    CREATE TABLE mytable_backup
    (
        Name VARCHAR(100),
        Age INT,
        Seq INT
    )
    GO
    

    Trigger

    create trigger myTrigger
    on mytable_backup
    instead of insert
    as
    begin
    ;WITH CTE
    AS
    (
        SELECT
            ROW_NUMBER() OVER(PARTITION BY inserted.Seq ORDER BY Age) AS RowNbr,
            inserted.*
        FROM
            inserted
        WHERE NOT EXISTS
        (
            SELECT
                NULL
            FROM
                mytable_backup
            WHERE
                mytable_backup.Seq=inserted.Seq
        )
    )
    insert into mytable_backup(Age,Name,Seq)
    SELECT
        CTE.Age,
        CTE.Name,
        cte.Seq
    FROM
        CTE
    WHERE
        CTE.RowNbr=1
    end
    GO
    

    Insert of test data

    INSERT INTO mytable_backup
    VALUES
        ('Gauraw',30,1),
        ('Gauraw',31,1),
        ('Kiran',28,3),
        ('Kiran',29,3),
        ('kiran',28,3),
        ('Venkatesh',20,4),
        ('Venkatesh',28,4)
    
    SELECT * FROM mytable_backup
    

    Drop of the database objects

    DROP TRIGGER myTrigger
    DROP TABLE mytable_backup
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to create Database as follows: USE Master GO IF NOT EXISTS(SELECT [Name] FROM
I've picked up some SQL similar to the following: IF EXISTS(SELECT name FROM tempdb..sysobjects
I use this command to drop a table in sql-server 2008 IF EXISTS(SELECT name
here is the query SELECT * FROM customers WHERE NOT EXISTS ( SELECT 1
I used to write my EXISTS checks like this: IF EXISTS (SELECT * FROM
SELECT A, B, C FROM TUser UNION IF EXISTS(SELECT dataUserId FROM TUserData WHERE DataId
I normally use the following code in SQL Server: IF EXISTS (SELECT * FROM
Question: If I add IF not exists to a create procedure as external name
How can the lookup order of 'where exists' be changed example SELECT name, dialcode
Can anyone explain why this works: use MyDb1 if NOT EXISTS (select * from

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.