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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:16:41+00:00 2026-05-10T18:16:41+00:00

I have a database full of customer data. It’s so big that it’s really

  • 0

I have a database full of customer data. It’s so big that it’s really cumbersome to operate on, and I’d rather just slim it down to 10% of the customers, which is plenty for development. I have an awful lot of tables and I don’t want to alter them all with ‘ON DELETE CASCADE’, especially because this is a one-time deal.

Can I do a delete operation that cascades through all my tables without setting them up first? If not, what is my best option?

  • 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. 2026-05-10T18:16:42+00:00Added an answer on May 10, 2026 at 6:16 pm

    Combining your advice and a script I found online, I made a procedure that will produce SQL you can run to perform a cascaded delete regardless of ON DELETE CASCADE. It was probably a big waste of time, but I had a good time writing it. An advantage of doing it this way is, you can put a GO statement between each line, and it doesn’t have to be one big transaction. The original was a recursive procedure; this one unrolls the recursion into a stack table.

    create procedure usp_delete_cascade (     @base_table_name varchar(200), @base_criteria nvarchar(1000) ) as begin     -- Adapted from http://www.sqlteam.com/article/performing-a-cascade-delete-in-sql-server-7     -- Expects the name of a table, and a conditional for selecting rows     -- within that table that you want deleted.     -- Produces SQL that, when run, deletes all table rows referencing the ones     -- you initially selected, cascading into any number of tables,     -- without the need for 'ON DELETE CASCADE'.     -- Does not appear to work with self-referencing tables, but it will     -- delete everything beneath them.     -- To make it easy on the server, put a 'GO' statement between each line.      declare @to_delete table (         id int identity(1, 1) primary key not null,         criteria nvarchar(1000) not null,         table_name varchar(200) not null,         processed bit not null,         delete_sql varchar(1000)     )      insert into @to_delete (criteria, table_name, processed) values (@base_criteria, @base_table_name, 0)      declare @id int, @criteria nvarchar(1000), @table_name varchar(200)     while exists(select 1 from @to_delete where processed = 0) begin         select top 1 @id = id, @criteria = criteria, @table_name = table_name from @to_delete where processed = 0 order by id desc          insert into @to_delete (criteria, table_name, processed)             select referencing_column.name + ' in (select [' + referenced_column.name + '] from [' + @table_name +'] where ' + @criteria + ')',                 referencing_table.name,                 0             from  sys.foreign_key_columns fk                 inner join sys.columns referencing_column on fk.parent_object_id = referencing_column.object_id                      and fk.parent_column_id = referencing_column.column_id                  inner join  sys.columns referenced_column on fk.referenced_object_id = referenced_column.object_id                      and fk.referenced_column_id = referenced_column.column_id                  inner join  sys.objects referencing_table on fk.parent_object_id = referencing_table.object_id                  inner join  sys.objects referenced_table on fk.referenced_object_id = referenced_table.object_id                  inner join  sys.objects constraint_object on fk.constraint_object_id = constraint_object.object_id             where referenced_table.name = @table_name                 and referencing_table.name != referenced_table.name          update @to_delete set             processed = 1         where id = @id     end      select 'print ''deleting from ' + table_name + '...''; delete from [' + table_name + '] where ' + criteria from @to_delete order by id desc end  exec usp_delete_cascade 'root_table_name', 'id = 123' 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a customer that has set up a full backup of the database
I have a database full of text fields that look like this: (paragraph of
I have a database full of two-dimensional data - points on a map. Each
We have an application that has a database full of polygons (currently stored as
Problem: I have a symfony project with a database full of production data I
I have a database full of simple note data, with columns for title, due
I have a database full of autopart numbers that needs old part numbers prices
I have a database full of dates that are in the format: MM/dd/yy. For
I have a database full of time-sensitive data, so on a daily basis I
I have a database full of classified adverts for computers for sale that have

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.