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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:39:20+00:00 2026-06-11T15:39:20+00:00

Say I have a self relation table as following : ID – Name –

  • 0

Say I have a self relation table as following :

   ID - Name - ParentID

Now everytime that users insert sth in this table I would like to check if the Name inserted is already in the
rows where ParentID equals to the inserted one , if true then rollback the transaction.

But the problem is when I check the rows with the parentID from the inserted table the inserted row is already in the main table too. So, the trigger always rolls back the transaction.

Here is my trigger :

ALTER TRIGGER TG_Check_Existance_In_myTbl
ON myTbl FOR INSERT,UPDATE AS


DEClARE @result BIT
DECLARE @numberOfRows INT
DECLARE @counter INT
DECLARE @names nVARCHAR (30)
DECLARE @name NVARCHAR (30)
SET @result = 0
SET @numberOfRows = (SELECT COUNT (Name)
                    FROM myTbl
                    WHERE ParentID IN
                        (
                            SELECT ParentID
                            FROM inserted
                        )
                    )
SET @counter = 1;
SELECT @name = Name 
FROM inserted
WHILE (@counter <= @numberOfRows)
BEGIN
    WITH Q
    AS
    (
    SELECT ROW_NUMBER()
    OVER (ORDER BY Name) 'Row', Name
    FROM myTbl WHERE ParentID IN
            (
                SELECT ParentID
                FROM inserted
            )
    )
    SELECT @names = Name
    FROM Q 
    WHERE Row = @counter
    IF @name = @names
    SET @result=1;
    SET @counter = @counter + 1
END
IF @result = 1
ROLLBACK TRAN
  • 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-11T15:39:21+00:00Added an answer on June 11, 2026 at 3:39 pm

    Unless I am missing something you are making this way too hard.

    Why don’t you use a unique constraint on the two columns?

    table_constraint (Transact-SQL)

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

Sidebar

Related Questions

Lets say I have the code: class NoneDict(dict): def __getitem__(self, name): try: return super(NoneDict,
Let's say I have the following code: class MyClass(object): def __init__(self, param): self.param =
So, say I have models like this: class Foo(Model): name = CharField(max_length=200) def latest_comment(self):
Let's say I have the following classes set up: class Foo: def __init__(self, frob,
Say you have a list of people. class Person: def __init___(self, name, id): self.name
Lets say I have two tables. class CreateUsers < ActiveRecord::Migration def self.up create_table :users
Say I have the following: class Parent(object): [...] def func(self, list): for item in
In short, say I have the following: import multiprocessing class Worker(multiprocessing.Process): def __init__(self): multiprocessing.Process.__init__(self)
Let's say we have the following code: class Post < ActiveRecord::Base def self.fix_published_times where(:published
Say I have an app with the following method strucutre : [self method1]; [self

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.