Solved

MS SQL - delete from multiple tables

Posted on 2010-09-21
12
752 Views
Last Modified: 2012-05-10
Hello ,
I need delete record from 12 tables, where the file number is = 19873

is there a way to create a procedure to delete from these tables?

thanks,
Caliu
0
Comment
Question by:Romacali
[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
  • 4
  • 3
  • 2
  • +2
12 Comments
 
LVL 6

Expert Comment

by:maxy88
ID: 33726785
You could create a SP to load data from the file into temp tables and then delete the records that you want from these temp tables. Then you can dump the temp tables back into the file and override anything that is there.
0
 
LVL 2

Accepted Solution

by:
marykdba earned 167 total points
ID: 33727793
See this excellent example:  http://www.sqlteam.com/article/performing-a-cascade-delete-in-sql-server-7

To upgrade this to SQL 2005 or SQL 2008, do the following:

Change this clause:

FROM dbo.sysforeignkeys FK  
INNER JOIN dbo.syscolumns SC1 ON FK.fkeyid = SC1.id
                              AND FK.fkey = SC1.colid
INNER JOIN dbo.syscolumns SC2 ON FK.rkeyid = SC2.id
                              AND FK.rkey = SC2.colid
INNER JOIN dbo.sysobjects SO1 ON FK.fkeyid = SO1.id
INNER JOIN dbo.sysobjects SO2 ON FK.rkeyid = SO2.id
INNER JOIN dbo.sysobjects FO ON FK.constid = FO.id

to this:

FROM sys.foreign_key_columns FK  
INNER JOIN sys.columns SC1 ON FK.parent_object_id = SC1.object_id AND FK.parent_column_id = SC1.column_id
INNER JOIN sys.columns SC2 ON FK.referenced_object_id = SC2.object_id AND FK.referenced_column_id = SC2.column_id
INNER JOIN sys.objects SO1 ON FK.parent_object_id = SO1.object_id
INNER JOIN sys.objects SO2 ON FK.referenced_object_id = SO2.object_id
INNER JOIN sys.objects FO ON FK.constraint_object_id = FO.object_id

The parameter @cCriteria needs a complete clause, such as 'FileNumber = 19873'.
0
 
LVL 6

Expert Comment

by:maxy88
ID: 33727863
I think I misunderstood the question. Could you clarify if you want to delete records from a file or if you just want to delete records from a number of linked tables where some filter condition applies?
0
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
LVL 2

Expert Comment

by:marykdba
ID: 33728290
P.S.  My solution depends on the fact that you have all the foreign keys set up for all the tables you wish to reference.
0
 
LVL 75

Assisted Solution

by:Anthony Perkins
Anthony Perkins earned 167 total points
ID: 33730443
Something like this perhaps:
CREATE PROCEDURE usp_DeleteFileNumber
               @FileNumber integer

AS

SET NOCOUNT ON

DELETE TableName1
WHERE FileNumber = @FileNumber

DELETE TableName2
WHERE FileNumber = @FileNumber
 
DELETE TableName3
WHERE FileNumber = @FileNumber

DELETE TableName4
WHERE FileNumber = @FileNumber

...

Open in new window

0
 
LVL 12

Assisted Solution

by:jagssidurala
jagssidurala earned 166 total points
ID: 33732058
The best way of deletion steps for multiple tables:

STEP1: Add a foreign key relationship to all the child tables with main table on Filenumber column.

STEP2: In relationships we have an option called "Delete Rule" under INSERT And UPDATE Specification. make this to "Cascade" for all ralationships(all tables).

STEP3: Just write a deletion statementt by filenumber for main table then the child tables data will delete automatically. no need to write delete statement for every table.

delete-rule-cscade.jpg
0
 
LVL 2

Expert Comment

by:marykdba
ID: 33738646
jaqssidurala:

Your solution is the simplest yet the most dangerous.  Sometimes we don't want a user to delete the record if certain conditions exist, yet we still want to maintain referential integrity.  Using the delete rule for cascading deletes should be implemented VERY cautiously.  I would also recommend audit tables as a backup.  
0
 
LVL 12

Expert Comment

by:jagssidurala
ID: 33763016
Do not give cascade option to all tables, should give to the tables which do not have dependency other tables.
For Example:
We have Customer, CustomerOrder, CustomerAddress, CustomerContact tables.

we can put cascade deletion for customeraddress, customercontact tables  because address and contact can be deleted but orders cannot be deleted as we have to track the orders(here reference integrity will work for the orders)
0
 
LVL 2

Expert Comment

by:marykdba
ID: 33764893
jaqssidurala,

Good point.  I was thinking you were putting cascade delete on ALL related tables.  From what you said, judicious use of cascading deletes is a good way to go.  
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 33765671
Now whatever happened to the author ...
0
 

Author Closing Comment

by:Romacali
ID: 33782144
it worked
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 33784963
What "worked"?  Was it http:#a33727793, http:#a33730443 or http:#a33732058 ?
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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 SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

752 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