开发者

SQL Trigger deleting from multiple tables

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 delete开发者_高级运维d 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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜