Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Delete from multiple table at once in sql server

Author  Topic 

kamii47
Constraint Violating Yak Guru

353 Posts

Posted - 2013-09-09 : 09:43:47
Can i Delete from multiple tables at once in sql server
I think something like this is available in mysql

Delete fe ,fd, fa,sf, sfsm
From dbo.FieldEnumeration As fe
Inner Join dbo.FieldDependency As fd
On fe.FieldEnumerationId = fd.FkFieldEnumerationId
Inner Join dbo.SimpleFieldDetail As sf
On fe.FkSimpleFieldDetailId = sf.SimpleFieldDetailId
Inner Join dbo.FieldAttributes As fa
On sf.SimpleFieldDetailId = fa.FkSimpleFieldDetailId
Inner Join dbo.SimpleFieldSectionMapper sfsm
On fd.ChildFieldId = sfsm.SimpleFieldSectionMappingId
And sf.SimpleFieldDetailId = sfsm.FkSimpleFieldDetailId
Inner Join dbo.SourceSection
On dbo.SimpleFieldSectionMapper.FkSourceSectionId = dbo.SourceSection.SourceSectionId
Inner Join dbo.TemplateSource
On dbo.SourceSection.FkTemplateSourceId = dbo.TemplateSource.TemplateSourceId
Inner Join dbo.Template tm
On dbo.TemplateSource.FkTemplateId = tm.TemplateId
Where tm.TemplateId = 1

Any idea how easily i can do this

Kamran Shahid
Principle Engineer Development
(MCSD.Net,MCPD.net)

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-09-09 : 10:26:26
T-SQL does not allow you to delete from multiple tables in one delete statement. If you want to preserve the atomicity of the deletions, the recommended way is to use a try-catch block and an explicit transaction.

You can use joins to pick up the rows that you want to delete, but the deletion has to be targeted at a single table.

T-SQL also supports cascade deletes - i.e., when you delete data from a table, you can set it up such that rows in other tables that are dependent on the row to be deleted via foreign key constraints will also get deleted.

Go to Top of Page

kamii47
Constraint Violating Yak Guru

353 Posts

Posted - 2013-09-10 : 01:07:57
I know about the cascade delete but at the moment i needed the deletion with manual query. Could u help translating the above query into separate deletion queries.
I know i might needed the temprorary table to first select the ids into them then delete one by one


Kamran Shahid
Principle Engineer Development
(MCSD.Net,MCPD.net)

Go to Top of Page
   

- Advertisement -