Solved

MS SQL - delete from multiple tables

Posted on 2010-09-21
12
719 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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.

708 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

14 Experts available now in Live!

Get 1:1 Help Now