Solved

Delete all records from all tables - conditionally

Posted on 2014-12-17
6
58 Views
Last Modified: 2014-12-22
Hi,

In my SQL Server database I have numerous tables.
Some tables (NOT ALL) have a boolean (bit) field called "IsVerwijderd".

I want to delete all records from all tables inside the database where field "IsVerwijderd" is present and is set to TRUE.

Does anybody have a solution for this?
0
Comment
Question by:Delphiwizard
  • 2
  • 2
  • 2
6 Comments
 
LVL 33

Expert Comment

by:ste5an
ID: 40504561
CAVEAT: Make a backup first!

EXECUTE sp_msforeachtable 'DELETE FROM ? WHERE IsVerwijderd = 1;';

Open in new window

0
 
LVL 48

Accepted Solution

by:
Vitor Montalvão earned 500 total points
ID: 40504629
Try this script:
DECLARE @CommDEL NVARCHAR(MAX)=N''

SELECT @CommDEL = @CommDEL + N'DELETE FROM ' + SCHEMA_NAME(o.schema_id) + '.' + o.name + ' WHERE IsVerwijderd=1;' + CHAR(13) + CHAR(10) 
FROM sys.objects o
	INNER JOIN sys.columns c ON c.object_id=o.object_id
WHERE o.object_id > 255 AND o.type='U' AND c.name='IsVerwijderd'

EXEC sp_executesql @CommDEL 

Open in new window

0
 

Author Comment

by:Delphiwizard
ID: 40504645
@ste5an: Your solution does do the job, but it's not very clean.
As there are tables that do not contain field IsVerwijderd, executing this code will generate numerous errormessages.
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 48

Expert Comment

by:Vitor Montalvão
ID: 40504651
As there are tables that do not contain field IsVerwijderd, executing this code will generate numerous errormessages.
That's why I wrote the script I posted. Only runs the delete command on tables that has the field.
0
 
LVL 33

Expert Comment

by:ste5an
ID: 40504684
You didn't ask for a clean version... There can be always errors caused by triggers or permissions or referential integrity.

The only clean solution is to script a DELETE per table without dynamic SQL. In the correct order regarding to FK relationships and containing the necessary conditions to avoid triggers or permission errors.

Or in simple  words: There is no general clean solution.
0
 

Author Closing Comment

by:Delphiwizard
ID: 40512788
This works fine for me. Thank you.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question