Solved

MS SQL - delete from multiple tables

Posted on 2010-09-21
12
738 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DTS Connection Failed 7 69
Convert formula to max at a specific date by month 18 18
Sql server insert 13 29
insert wont work in SQL 14 21
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…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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.

770 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