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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:16:32+00:00 2026-05-25T17:16:32+00:00

I have 2 tables with relation in between. table one is holding groups with

  • 0

I have 2 tables with relation in between. table one is holding groups with groupid. table 2 is holding the relation between the 2 groups.

table 1 
GroupId, Name, Description, .......

table 2
MasterGroupId, SubGroupId, sequenceNumber

When preforming a deletion of a group out of table 1 all of the related groups has to be deleted to.

this includes the subgroups under the group and there subgroups recursive.

example of the data in both tables before delete

Table Tbl_TemplateListGroup

GroupId |Name      |Description   |TemplateListId
100 | Group 1   | NULL   | 6
101 | Group 2   | NULL   | 6
102 | Group 11  | NULL  | 6
103 | Group 12  | NULL  | 6
104 | Group 13  | NULL  | 6
105 | Group 131 | NULL  | 6
106 | Group 1311    | NULL  | 6
107 | Group111  | NULL  | 6

Table Tbl_TemplateListGroupGroup

Master  | Sub     SequenceNumber
Group   | Group
Id      | Id
100 | 102   | 1
100 | 103   | 2
100 | 104   | 3
102 | 107   | 1
104 | 105   | 1
105 | 106   | 1

example of data after delete
Table Tbl_TemplateListGroup

GroupId |Name      |Description   |TemplateListId
100 |Group 1    |           |6
101 |Group 2    |           |6
102 |Group 11   |   |6
103 |Group 12   |   |6
107 |Group111   |   |6

Tabel Tbl_TemplateListGroupGroup

Master  Sub     SequenceNumber
Group   Group
Id       Id
100 |102    |1
100 |103    |2
102 |107    |1

the delete statement

delete from tbl_TemplateListGroup where GroupId = 104

The trigger is now :

CREATE TRIGGER TR_TemplateListGroupDelGroup                        
on Tbl_TemplateListGroup                                           
INSTEAD OF DELETE                                                  
AS                                                                 
  BEGIN                                                            
    SET NOCOUNT ON;                                                

    BEGIN                                                          
      SELECT GroupId INTO tmpTbl FROM Tbl_TemplateListGroup        
      WHERE GroupId in (SELECT SubGroupId FROM Tbl_TemplateListGroupGroup 
                        WHERE MasterGroupId in ( select d.GroupId from deleted d ))  

      DELETE FROM Tbl_TemplateListGroupGroup                       
      WHERE SubGroupId in ( select d.GroupId from deleted d )      

      DELETE FROM Tbl_TemplateListGroupGroup                       
      WHERE MasterGroupId in ( select d.GroupId from deleted d )   

      DELETE FROM Tbl_TemplateListGroup                            
      WHERE GroupId in ( select d.GroupId from deleted d )         

      DELETE FROM Tbl_TemplateListGroup                            
      WHERE GroupId in (select GroupId FROM tmpTbl)                

      DROP TABLE tmpTbl;                                           
    END                                                            
  END                                                              

this is not working for 2 reasons

  1. The user has no create table rights so he can’t create a temp table. ( is there an other way to get that data).
  2. I think that the group with groupid 105 is restricting the delete because the trigger was working (in sa mode) before I have added that item with its relation.

For now I delete both tables and fill them again with the correct data. I would like to solve that in the DB.

  • 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-05-25T17:16:33+00:00Added an answer on May 25, 2026 at 5:16 pm

    Unfortunately, because there are two tables involved, a temp table will still be required. Something like:

    CREATE TABLE #GroupIDs (ID int)
    
    ;WITH Closure AS (
        SELECT GroupId from deleted
        UNION ALL
        SELECT SubGroupId from Tbl_TemplateListGroupGroup lgg inner join Closure c on lgg.MasterGroupId = c.GroupId
    )
    INSERT INTO #GroupIDs (ID) SELECT GroupID from Closure
    
    DELETE FROM Tbl_TemplateListGroupGroup where MasterGroupID in (select ID from #GroupIDs)
    DELETE FROM Tbl_TemplateListGroup where GroupID in (select ID from #GroupsIDs)
    

    This uses a Recursive Common Table Expression to compute the closure over the subgroup table. These are available from SQL Server 2005 onwards.

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

Sidebar

Related Questions

I have a legacy data base and a relation one-to-one between two tables. The
I have two tables made in access. One table (Owner) contains: ownerID , name
I have 3 tables: CRSTasks (ID,parentID) CRSTaskReceivers (ID,tskID,receiverID) UserNames (id,name) ...relation between CRSTasks and
I have a one-to-one relation between an Account and a User table, I'm trying
I have a one-to-many relation between Parent and Child table. In the parent object
We have two tables Family and Member, the relation between these two is Family
Let's say, I have two tables, entities and tags. Relation between them is expressed
I have the following table, Persons_Companies , that shows a relation between persons and
I have 2 tables that their rows have one on one relation.. For you
Lets say I have two tables - child and parent with many-to-one relation. What

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.