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

  • Home
  • SEARCH
  • 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 8770547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:34:37+00:00 2026-06-13T17:34:37+00:00

Say I have two tables. Errors and ErrorsDetails . So I have one Errors

  • 0

Say I have two tables. Errors and ErrorsDetails. So I have one Errors record that could contain many ErrorDetails records.

The users will periodically delete the detail records through an application they use. When all detail records are cleaned up (deleted), I would like to automatically delete the master record too.

What would be cool would be one SQL delete statement that would delete the details and the master if no more details are owned by the master. But I can’t seem to get a clean SQL statement that does that. I’d rather not poll the master for details after every delete.

Errors

ID        Error
1         Import Error
2         Export Error

ErrorsDetails

ID        ErrorsID        DetailedInfo
1         1               Couldn't import file because of FTP error
2         1               Couldn't import file because of permission error
3         2               Couldn't export file because of directory doesn't exist

I’ve tried adding a cascade delete constraint like so but it didn’t seem to work.

alter table dbo.ErrorsDetails
    add constraint FK_Delete_Master
        foreign key (ErrorsID) references dbo.Errors(ErrorsID) on delete cascade

Thanks for any suggestions.

UPDATE

It’s been mentioned to use triggers. I thought about that but didn’t mention it. A trigger would certainly do the job. Pros/Cons of triggers for this scenario?

  • 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-13T17:34:39+00:00Added an answer on June 13, 2026 at 5:34 pm

    You can use triggers
    or

    declare @ErrorsId int
    select @ErrorsId = ErrorsId from ErrorsDetails where ID = @id
    delete from ErrorsDetails where ID = @id
    
    declare @errorDetailCount int
    select @errorDetailCount = count(*) from ErrorsDetails where ErrorsId = @ErrorsId 
    
    if (@errorDetailCount = 0)
    begin
     delete from Errors where ID = @ErrorsId 
    end
    

    UPDATED:
    trigger:

    create trigger [dbo].[ErrorsDetails_Delete_Trigger] on [dbo].[ErrorsDetails] 
    instead of delete
    as 
    begin          
        declare @ErrorsId int
        select @ErrorsId = ErrorsId from deleted
        delete from ErrorsDetails where ID = deleted.ID
    
        declare @errorDetailCount int
        select @errorDetailCount = count(*) from ErrorsDetails where ErrorsId = @ErrorsId 
    
        if (@errorDetailCount = 0)
        begin
         delete from Errors where ID = @ErrorsId 
        end     
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say we have two tables in an MS Access db: Service Users: | ID
Lets say I have two tables - Cat and Cat owner that are linked
Let's say I have two tables, users and notes. Let's say the schemas look
Let's say I have two tables that look like this: TH TH TH TH
I have a table, say STUDENTS, that is related one-to-many to another table, CLASSES.
Say I have two tables, users and groups each of which has an auto-incrementing
Say I have two tables called A (fields: id, phase, name) and B(fields: id,
Lets say I have two tables tblA ( tableAID INT IDENTITY(1,1), foo VARCHAR(100)) tblB
Let's say i have two tables in db: Car and Part. Car owns arbitrialy
Let's say I have two tables lnglngdef: lngId | lngName 1 Korean 2 Spanish

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.