Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MS SQL - delete from multiple tables

Posted on 2010-09-21
12
Medium Priority
?
810 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 668 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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
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 668 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 664 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
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.
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…

963 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