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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:58:50+00:00 2026-06-17T12:58:50+00:00

Suppose I have table A and B. Table A has a FK to Table

  • 0

Suppose I have table A and B. Table A has a FK to Table B.

I could categorize records in B as ‘referenced’ and ‘unreferenced’, meaning that there’s some record in the database that has a FK pointing to that record or not, respectively.

I want to find a scheme-independent way to delete all rows in B which are unreferenced.
Doing a simple DELETE of table B without any constraints (because I want to be scheme-independent, i.e. a general way), almost certainly, it will fail.

If it could also be vendor independent would be great, but I feel I’m asking for too much.

Edit: Maybe I wasn’t clear. When I say I don’t want to depend on the scheme, I refer avoiding naming column names from other tables when doing the ‘DELETE’. Just deleting records of B which can be deleted without breaking DB consistency.

  • 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-17T12:58:51+00:00Added an answer on June 17, 2026 at 12:58 pm

    The slow way:

    declare del_cur cursor
    local
    forward_only
    static
    scroll_locks
    for
      select 0 from B
    ;
    
    declare @foo int;
    
    open del_cur;
    
    fetch next from del_cur into @foo;
    while @@fetch_status = 0
    begin
      begin try
        delete from B where current of del_cur;
      end try
      begin catch
        -- It was referenced, skip
      end catch;
    
      fetch next from del_cur into @foo;
    end;
    
    close del_cur;
    deallocate del_cur;
    

    A faster way would be examining the existing foreign keys and building a dynamic SQL statement that includes their column names.

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

Sidebar

Related Questions

Suppose I have employee and department table, employee has foreign key departmentID that is
I have this homework question: Assume a Database table has 0 records at time
Suppose that I have a customers table which has an account_number and a billing_account_number
Suppose that I have two Hibernate POJOs: Customer and Order. The Order table has
Let's suppose I have a table in my database with 1.000.000 records. If I
I have a database which has records with several fields containing some info. To
I have requirement like, suppose I have a 'property' table which has 'ListingKey' field
Suppose I have two tables that are linked (one has a foreign key to
Suppose I have table A with a field that can be either 1 or
Suppose I have a table Item (Id int Primary Key, Number INT) having records

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.