Solved

MS SQL - delete from multiple tables

Posted on 2010-09-21
12
727 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
  • 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
 
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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
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…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

937 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now