Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / Programming / SQL / July 2008

Tip: Looking for answers? Try searching our database.

Delete records

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
formcreator - 25 Jul 2008 20:28 GMT
Hello All,
   I have three tables which are joined by a common field CS_Survey_rec ID.
How do I delete records from all tables where cs_Survey_RecID >17. This is
the first time I am deleting, so please help I don't want to delete the wrong
records

Thank You
Shri
Aaron Bertrand [SQL Server MVP] - 25 Jul 2008 20:34 GMT
You can only DELETE from one table at a time, so you will need to issue
three DELETE commands.  And depending on how the tables are related (e.g.
are there foreign key constraints?) you may have to do so in a certain
order.

DELETE dbo.Child1 WHERE CS_Survey_RecID > 17;
DELETE dbo.Child2 WHERE CS_Survey_RecID > 17;
DELETE dbo.Parent WHERE CS_Survey_RecID > 17;

You can optionally wrap this in a transaction, add error handling, etc.

A

On 7/25/08 3:28 PM, in article
BEE17A0A-3C00-41E6-9A6C-01270AC15D1D@microsoft.com, "formcreator"

> Hello All,
>     I have three tables which are joined by a common field CS_Survey_rec ID.
[quoted text clipped - 4 lines]
> Thank You
> Shri
vinu - 28 Jul 2008 09:16 GMT
Shri,

if the tables are linked and the CASCADE DELETE is set then you only need to
delete form the parent table, sql server will delete the details record from
the child table
see the eg

CREATE TABLE [dbo].[Table1] (
[id] [int] NOT NULL ,
[c] [char] (10) COLLATE Latin1_General_CI_AS NULL
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[Table2] (
[idd] [int] NULL ,
[id] [int] NULL ,
[asd] [char] (10) COLLATE Latin1_General_CI_AS NULL
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[Table1] ADD
CONSTRAINT [PK_Table1] PRIMARY KEY  CLUSTERED
(
 [id]
)  ON [PRIMARY]
GO

ALTER TABLE [dbo].[Table2] ADD
CONSTRAINT [FK_Table2_Table1] FOREIGN KEY
(
 [id]
) REFERENCES [dbo].[Table1] (
 [id]
) ON DELETE CASCADE
GO

table1

id          c
----------- ----------
1           sdf
2           ddd
3           tyutyu
4           opiop

table2
idd         id          asd
----------- ----------- ----------
1           1           bnbbn
2           1           ytyyy
1           2           xxxxx
5           3           rrr

now if I delete id=2 from table1, the linked record in table2(idd=1 amd
id=2,asd=xxxxx) will also get deleted

delete from table1 where id=2

table1
id          c
----------- ----------
1           sdf
3           tyutyu
4           opiop

table2

idd         id          asd
----------- ----------- ----------
1           1           bnbbn
2           1           ytyyy
5           3           rrr

Signature

vinu
http://oneplace4sql.blogspot.com/

> You can only DELETE from one table at a time, so you will need to issue
> three DELETE commands.  And depending on how the tables are related (e.g.
[quoted text clipped - 23 lines]
>> Thank You
>> Shri
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.