?
Solved

Delete all records from all tables - conditionally

Posted on 2014-12-17
6
Medium Priority
?
64 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 35

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 51

Accepted Solution

by:
Vitor Montalvão earned 2000 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
Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card.

 
LVL 51

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 35

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

Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Suggested Courses

762 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