Deleting from multiple tables using a left join in one query
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I am wondering if I can do the following, I have 3 tables that I want to delete data from : Table1, Table2, Table3
There will always be a record in Table1
and there might or might not be an associated record in Table2
and Table3
.
So I thought I could do the following:
DELETE FROM dbo.Table1, dbo.Table2, dbo.Table3
FROM Table1 t1
LEFT JOIN dbo.Table2 t2 ON t1.Id = t2.Id
LEFT JOIN dbo.Table3 t3 ON t3.Id = t1.Id
WHERE t1.Id = @Id;
But I get an Incorrect syntax near ','.
because of the DELETE FROM dbo.Table1, dbo.Table2, dbo.Table3
line.
The desired results is to have all the data from the 3 tables deleted with the Where
clause.
I'm not sure how to acomplish this.
sql sql-server tsql
|
show 3 more comments
I am wondering if I can do the following, I have 3 tables that I want to delete data from : Table1, Table2, Table3
There will always be a record in Table1
and there might or might not be an associated record in Table2
and Table3
.
So I thought I could do the following:
DELETE FROM dbo.Table1, dbo.Table2, dbo.Table3
FROM Table1 t1
LEFT JOIN dbo.Table2 t2 ON t1.Id = t2.Id
LEFT JOIN dbo.Table3 t3 ON t3.Id = t1.Id
WHERE t1.Id = @Id;
But I get an Incorrect syntax near ','.
because of the DELETE FROM dbo.Table1, dbo.Table2, dbo.Table3
line.
The desired results is to have all the data from the 3 tables deleted with the Where
clause.
I'm not sure how to acomplish this.
sql sql-server tsql
Remove the first 'FROM' clause and try
– Abdul Rasheed
Nov 23 '18 at 11:54
2
SQL Server only allows you to delete from one table at a time. Perhaps you want cascading foreign key constraints.
– Gordon Linoff
Nov 23 '18 at 11:54
@AbdulRasheed that doesn't work, I tried that
– Jamie Rees
Nov 23 '18 at 11:55
@GordonLinoff let's say this is quite an old database and db integrity wasn't that thought out of at the time...
– Jamie Rees
Nov 23 '18 at 11:56
First you need to remove the first from, 2nd SQL Server allow to delete just from one table, 3rd you can useOUTPUT
clause to delete from 3 tables, or even so byON DELETE CASCADE
– Sami
Nov 23 '18 at 11:56
|
show 3 more comments
I am wondering if I can do the following, I have 3 tables that I want to delete data from : Table1, Table2, Table3
There will always be a record in Table1
and there might or might not be an associated record in Table2
and Table3
.
So I thought I could do the following:
DELETE FROM dbo.Table1, dbo.Table2, dbo.Table3
FROM Table1 t1
LEFT JOIN dbo.Table2 t2 ON t1.Id = t2.Id
LEFT JOIN dbo.Table3 t3 ON t3.Id = t1.Id
WHERE t1.Id = @Id;
But I get an Incorrect syntax near ','.
because of the DELETE FROM dbo.Table1, dbo.Table2, dbo.Table3
line.
The desired results is to have all the data from the 3 tables deleted with the Where
clause.
I'm not sure how to acomplish this.
sql sql-server tsql
I am wondering if I can do the following, I have 3 tables that I want to delete data from : Table1, Table2, Table3
There will always be a record in Table1
and there might or might not be an associated record in Table2
and Table3
.
So I thought I could do the following:
DELETE FROM dbo.Table1, dbo.Table2, dbo.Table3
FROM Table1 t1
LEFT JOIN dbo.Table2 t2 ON t1.Id = t2.Id
LEFT JOIN dbo.Table3 t3 ON t3.Id = t1.Id
WHERE t1.Id = @Id;
But I get an Incorrect syntax near ','.
because of the DELETE FROM dbo.Table1, dbo.Table2, dbo.Table3
line.
The desired results is to have all the data from the 3 tables deleted with the Where
clause.
I'm not sure how to acomplish this.
sql sql-server tsql
sql sql-server tsql
asked Nov 23 '18 at 11:53
Jamie ReesJamie Rees
4,77712960
4,77712960
Remove the first 'FROM' clause and try
– Abdul Rasheed
Nov 23 '18 at 11:54
2
SQL Server only allows you to delete from one table at a time. Perhaps you want cascading foreign key constraints.
– Gordon Linoff
Nov 23 '18 at 11:54
@AbdulRasheed that doesn't work, I tried that
– Jamie Rees
Nov 23 '18 at 11:55
@GordonLinoff let's say this is quite an old database and db integrity wasn't that thought out of at the time...
– Jamie Rees
Nov 23 '18 at 11:56
First you need to remove the first from, 2nd SQL Server allow to delete just from one table, 3rd you can useOUTPUT
clause to delete from 3 tables, or even so byON DELETE CASCADE
– Sami
Nov 23 '18 at 11:56
|
show 3 more comments
Remove the first 'FROM' clause and try
– Abdul Rasheed
Nov 23 '18 at 11:54
2
SQL Server only allows you to delete from one table at a time. Perhaps you want cascading foreign key constraints.
– Gordon Linoff
Nov 23 '18 at 11:54
@AbdulRasheed that doesn't work, I tried that
– Jamie Rees
Nov 23 '18 at 11:55
@GordonLinoff let's say this is quite an old database and db integrity wasn't that thought out of at the time...
– Jamie Rees
Nov 23 '18 at 11:56
First you need to remove the first from, 2nd SQL Server allow to delete just from one table, 3rd you can useOUTPUT
clause to delete from 3 tables, or even so byON DELETE CASCADE
– Sami
Nov 23 '18 at 11:56
Remove the first 'FROM' clause and try
– Abdul Rasheed
Nov 23 '18 at 11:54
Remove the first 'FROM' clause and try
– Abdul Rasheed
Nov 23 '18 at 11:54
2
2
SQL Server only allows you to delete from one table at a time. Perhaps you want cascading foreign key constraints.
– Gordon Linoff
Nov 23 '18 at 11:54
SQL Server only allows you to delete from one table at a time. Perhaps you want cascading foreign key constraints.
– Gordon Linoff
Nov 23 '18 at 11:54
@AbdulRasheed that doesn't work, I tried that
– Jamie Rees
Nov 23 '18 at 11:55
@AbdulRasheed that doesn't work, I tried that
– Jamie Rees
Nov 23 '18 at 11:55
@GordonLinoff let's say this is quite an old database and db integrity wasn't that thought out of at the time...
– Jamie Rees
Nov 23 '18 at 11:56
@GordonLinoff let's say this is quite an old database and db integrity wasn't that thought out of at the time...
– Jamie Rees
Nov 23 '18 at 11:56
First you need to remove the first from, 2nd SQL Server allow to delete just from one table, 3rd you can use
OUTPUT
clause to delete from 3 tables, or even so by ON DELETE CASCADE
– Sami
Nov 23 '18 at 11:56
First you need to remove the first from, 2nd SQL Server allow to delete just from one table, 3rd you can use
OUTPUT
clause to delete from 3 tables, or even so by ON DELETE CASCADE
– Sami
Nov 23 '18 at 11:56
|
show 3 more comments
4 Answers
4
active
oldest
votes
You can only delete from one table at a time. Presumably, you want cascading constraints:
alter table2 add constraint fk_table2_table1
foreign key (id) references table1(id)
on delete cascade;
alter tablee add constraint fk_tablee_table1
foreign key (id) references table1(id)
on delete cascade;
Foreign keys ensure data integrity. Cascading constraints mean that when the primary key is deleted, then the corresponding rows in the other tables are also removed.
Yeah ideally this would work, but due to the size of the database and the importance of the data, currently cascade deleting is not an option.
– Jamie Rees
Nov 23 '18 at 11:59
1
@JamieRees: you can always add the constraintWITH NOCHECK
so it isn't validated for current data -- and drop it afterwards if you don't like the idea of having it stick around afterwards. This makes the size of the current data largely irrelevant. The only sticking points then are out-of-order inserts, if the system uses those, and possible failure if rows are updated that refer to non-existent parent records (which is typically undesirable anyway, and would justify a preceding scrubbing step, or at least a query to see if there are any).
– Jeroen Mostert
Nov 23 '18 at 12:12
add a comment |
A couple of methods to this. Personally, I would recommend the SP over the trigger; like others I'm not a huge fan of triggers as they can often be a "hidden" entity (far too many times have I been unable to work out why something isn't working to find it it was a trigger I wasn't aware of).
Anyway, there trigger method would be:
CREATE TRIGGER Cascade_Table2_Table3 ON dbo.Table1
AFTER DELETE
AS
DELETE T2
FROM Table2 T2
JOIN deleted d ON t2.Id = d.Id;
DELETE T3
FROM Table3 T3
JOIN deleted d ON t3.Id = d.Id;
GO
However, as you're using a single value, then a parametrised Stored Procedure would be far better; and then you DENY
users from running a DELETE
on the tables:
CREATE PROC Delete_T1T2T3 @ID int AS
DELETE FROM dbo.Table1
WHERE ID = @ID;
DELETE FROM dbo.Table2
WHERE ID = @ID;
DELETE FROM dbo.Table3
WHERE ID = @ID;
GO
add a comment |
can you use a trigger on table1?, so you can delete records only from table1 and then let the trigger delete stuff from table2 and table3.
Something like:
CREATE TRIGGER [dbo].[T_deleteT2_t3]
ON [dbo].[Table1]
AFTER DELETE
AS
BEGIN
delete from table2 where id = select id from deleted
delete from table3 where id = select id from deleted
END
1
It's a valid answer, but I personally don't like the use of triggers in this scenario, triggers in general i'm not a fan of due to their 'hidden' nature.
– Jamie Rees
Nov 23 '18 at 12:05
What if you delete 100 rows from this table?
– Sami
Nov 23 '18 at 12:05
3
@JamieRees How that can be a valid answer while it hasid = select id from deleted
?
– Sami
Nov 23 '18 at 12:06
@Sami I overlooked that, good point
– Jamie Rees
Nov 23 '18 at 12:06
@Sami Its preudocode just to know if the answer could fit him
– picklerick
Nov 23 '18 at 12:08
add a comment |
Here is another way using OUTPUT
clause
CREATE TABLE T1( ID INT);
CREATE TABLE T2( ID INT);
CREATE TABLE T3( ID INT);
INSERT INTO T1 VALUES (1), (2);
INSERT INTO T2 VALUES (1), (3);
INSERT INTO T3 VALUES (1), (4);
BEGIN TRAN;
DECLARE @Out TABLE (IDs INT);
DELETE T1
OUTPUT DELETED.ID INTO @Out
FROM T1 INNER JOIN T2 ON T1.ID = T2.ID;
DELETE T2
FROM T2 INNER JOIN @Out O ON T2.ID = O.IDs;
DELETE T3
FROM T3 INNER JOIN @Out O ON T3.ID = O.IDs;
COMMIT TRAN;
SELECT *
FROM T1;
SELECT *
FROM T2;
SELECT *
FROM T3;
Demo
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53446226%2fdeleting-from-multiple-tables-using-a-left-join-in-one-query%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can only delete from one table at a time. Presumably, you want cascading constraints:
alter table2 add constraint fk_table2_table1
foreign key (id) references table1(id)
on delete cascade;
alter tablee add constraint fk_tablee_table1
foreign key (id) references table1(id)
on delete cascade;
Foreign keys ensure data integrity. Cascading constraints mean that when the primary key is deleted, then the corresponding rows in the other tables are also removed.
Yeah ideally this would work, but due to the size of the database and the importance of the data, currently cascade deleting is not an option.
– Jamie Rees
Nov 23 '18 at 11:59
1
@JamieRees: you can always add the constraintWITH NOCHECK
so it isn't validated for current data -- and drop it afterwards if you don't like the idea of having it stick around afterwards. This makes the size of the current data largely irrelevant. The only sticking points then are out-of-order inserts, if the system uses those, and possible failure if rows are updated that refer to non-existent parent records (which is typically undesirable anyway, and would justify a preceding scrubbing step, or at least a query to see if there are any).
– Jeroen Mostert
Nov 23 '18 at 12:12
add a comment |
You can only delete from one table at a time. Presumably, you want cascading constraints:
alter table2 add constraint fk_table2_table1
foreign key (id) references table1(id)
on delete cascade;
alter tablee add constraint fk_tablee_table1
foreign key (id) references table1(id)
on delete cascade;
Foreign keys ensure data integrity. Cascading constraints mean that when the primary key is deleted, then the corresponding rows in the other tables are also removed.
Yeah ideally this would work, but due to the size of the database and the importance of the data, currently cascade deleting is not an option.
– Jamie Rees
Nov 23 '18 at 11:59
1
@JamieRees: you can always add the constraintWITH NOCHECK
so it isn't validated for current data -- and drop it afterwards if you don't like the idea of having it stick around afterwards. This makes the size of the current data largely irrelevant. The only sticking points then are out-of-order inserts, if the system uses those, and possible failure if rows are updated that refer to non-existent parent records (which is typically undesirable anyway, and would justify a preceding scrubbing step, or at least a query to see if there are any).
– Jeroen Mostert
Nov 23 '18 at 12:12
add a comment |
You can only delete from one table at a time. Presumably, you want cascading constraints:
alter table2 add constraint fk_table2_table1
foreign key (id) references table1(id)
on delete cascade;
alter tablee add constraint fk_tablee_table1
foreign key (id) references table1(id)
on delete cascade;
Foreign keys ensure data integrity. Cascading constraints mean that when the primary key is deleted, then the corresponding rows in the other tables are also removed.
You can only delete from one table at a time. Presumably, you want cascading constraints:
alter table2 add constraint fk_table2_table1
foreign key (id) references table1(id)
on delete cascade;
alter tablee add constraint fk_tablee_table1
foreign key (id) references table1(id)
on delete cascade;
Foreign keys ensure data integrity. Cascading constraints mean that when the primary key is deleted, then the corresponding rows in the other tables are also removed.
answered Nov 23 '18 at 11:57
Gordon LinoffGordon Linoff
794k37318422
794k37318422
Yeah ideally this would work, but due to the size of the database and the importance of the data, currently cascade deleting is not an option.
– Jamie Rees
Nov 23 '18 at 11:59
1
@JamieRees: you can always add the constraintWITH NOCHECK
so it isn't validated for current data -- and drop it afterwards if you don't like the idea of having it stick around afterwards. This makes the size of the current data largely irrelevant. The only sticking points then are out-of-order inserts, if the system uses those, and possible failure if rows are updated that refer to non-existent parent records (which is typically undesirable anyway, and would justify a preceding scrubbing step, or at least a query to see if there are any).
– Jeroen Mostert
Nov 23 '18 at 12:12
add a comment |
Yeah ideally this would work, but due to the size of the database and the importance of the data, currently cascade deleting is not an option.
– Jamie Rees
Nov 23 '18 at 11:59
1
@JamieRees: you can always add the constraintWITH NOCHECK
so it isn't validated for current data -- and drop it afterwards if you don't like the idea of having it stick around afterwards. This makes the size of the current data largely irrelevant. The only sticking points then are out-of-order inserts, if the system uses those, and possible failure if rows are updated that refer to non-existent parent records (which is typically undesirable anyway, and would justify a preceding scrubbing step, or at least a query to see if there are any).
– Jeroen Mostert
Nov 23 '18 at 12:12
Yeah ideally this would work, but due to the size of the database and the importance of the data, currently cascade deleting is not an option.
– Jamie Rees
Nov 23 '18 at 11:59
Yeah ideally this would work, but due to the size of the database and the importance of the data, currently cascade deleting is not an option.
– Jamie Rees
Nov 23 '18 at 11:59
1
1
@JamieRees: you can always add the constraint
WITH NOCHECK
so it isn't validated for current data -- and drop it afterwards if you don't like the idea of having it stick around afterwards. This makes the size of the current data largely irrelevant. The only sticking points then are out-of-order inserts, if the system uses those, and possible failure if rows are updated that refer to non-existent parent records (which is typically undesirable anyway, and would justify a preceding scrubbing step, or at least a query to see if there are any).– Jeroen Mostert
Nov 23 '18 at 12:12
@JamieRees: you can always add the constraint
WITH NOCHECK
so it isn't validated for current data -- and drop it afterwards if you don't like the idea of having it stick around afterwards. This makes the size of the current data largely irrelevant. The only sticking points then are out-of-order inserts, if the system uses those, and possible failure if rows are updated that refer to non-existent parent records (which is typically undesirable anyway, and would justify a preceding scrubbing step, or at least a query to see if there are any).– Jeroen Mostert
Nov 23 '18 at 12:12
add a comment |
A couple of methods to this. Personally, I would recommend the SP over the trigger; like others I'm not a huge fan of triggers as they can often be a "hidden" entity (far too many times have I been unable to work out why something isn't working to find it it was a trigger I wasn't aware of).
Anyway, there trigger method would be:
CREATE TRIGGER Cascade_Table2_Table3 ON dbo.Table1
AFTER DELETE
AS
DELETE T2
FROM Table2 T2
JOIN deleted d ON t2.Id = d.Id;
DELETE T3
FROM Table3 T3
JOIN deleted d ON t3.Id = d.Id;
GO
However, as you're using a single value, then a parametrised Stored Procedure would be far better; and then you DENY
users from running a DELETE
on the tables:
CREATE PROC Delete_T1T2T3 @ID int AS
DELETE FROM dbo.Table1
WHERE ID = @ID;
DELETE FROM dbo.Table2
WHERE ID = @ID;
DELETE FROM dbo.Table3
WHERE ID = @ID;
GO
add a comment |
A couple of methods to this. Personally, I would recommend the SP over the trigger; like others I'm not a huge fan of triggers as they can often be a "hidden" entity (far too many times have I been unable to work out why something isn't working to find it it was a trigger I wasn't aware of).
Anyway, there trigger method would be:
CREATE TRIGGER Cascade_Table2_Table3 ON dbo.Table1
AFTER DELETE
AS
DELETE T2
FROM Table2 T2
JOIN deleted d ON t2.Id = d.Id;
DELETE T3
FROM Table3 T3
JOIN deleted d ON t3.Id = d.Id;
GO
However, as you're using a single value, then a parametrised Stored Procedure would be far better; and then you DENY
users from running a DELETE
on the tables:
CREATE PROC Delete_T1T2T3 @ID int AS
DELETE FROM dbo.Table1
WHERE ID = @ID;
DELETE FROM dbo.Table2
WHERE ID = @ID;
DELETE FROM dbo.Table3
WHERE ID = @ID;
GO
add a comment |
A couple of methods to this. Personally, I would recommend the SP over the trigger; like others I'm not a huge fan of triggers as they can often be a "hidden" entity (far too many times have I been unable to work out why something isn't working to find it it was a trigger I wasn't aware of).
Anyway, there trigger method would be:
CREATE TRIGGER Cascade_Table2_Table3 ON dbo.Table1
AFTER DELETE
AS
DELETE T2
FROM Table2 T2
JOIN deleted d ON t2.Id = d.Id;
DELETE T3
FROM Table3 T3
JOIN deleted d ON t3.Id = d.Id;
GO
However, as you're using a single value, then a parametrised Stored Procedure would be far better; and then you DENY
users from running a DELETE
on the tables:
CREATE PROC Delete_T1T2T3 @ID int AS
DELETE FROM dbo.Table1
WHERE ID = @ID;
DELETE FROM dbo.Table2
WHERE ID = @ID;
DELETE FROM dbo.Table3
WHERE ID = @ID;
GO
A couple of methods to this. Personally, I would recommend the SP over the trigger; like others I'm not a huge fan of triggers as they can often be a "hidden" entity (far too many times have I been unable to work out why something isn't working to find it it was a trigger I wasn't aware of).
Anyway, there trigger method would be:
CREATE TRIGGER Cascade_Table2_Table3 ON dbo.Table1
AFTER DELETE
AS
DELETE T2
FROM Table2 T2
JOIN deleted d ON t2.Id = d.Id;
DELETE T3
FROM Table3 T3
JOIN deleted d ON t3.Id = d.Id;
GO
However, as you're using a single value, then a parametrised Stored Procedure would be far better; and then you DENY
users from running a DELETE
on the tables:
CREATE PROC Delete_T1T2T3 @ID int AS
DELETE FROM dbo.Table1
WHERE ID = @ID;
DELETE FROM dbo.Table2
WHERE ID = @ID;
DELETE FROM dbo.Table3
WHERE ID = @ID;
GO
edited Nov 23 '18 at 12:28
answered Nov 23 '18 at 12:18
LarnuLarnu
22.6k51933
22.6k51933
add a comment |
add a comment |
can you use a trigger on table1?, so you can delete records only from table1 and then let the trigger delete stuff from table2 and table3.
Something like:
CREATE TRIGGER [dbo].[T_deleteT2_t3]
ON [dbo].[Table1]
AFTER DELETE
AS
BEGIN
delete from table2 where id = select id from deleted
delete from table3 where id = select id from deleted
END
1
It's a valid answer, but I personally don't like the use of triggers in this scenario, triggers in general i'm not a fan of due to their 'hidden' nature.
– Jamie Rees
Nov 23 '18 at 12:05
What if you delete 100 rows from this table?
– Sami
Nov 23 '18 at 12:05
3
@JamieRees How that can be a valid answer while it hasid = select id from deleted
?
– Sami
Nov 23 '18 at 12:06
@Sami I overlooked that, good point
– Jamie Rees
Nov 23 '18 at 12:06
@Sami Its preudocode just to know if the answer could fit him
– picklerick
Nov 23 '18 at 12:08
add a comment |
can you use a trigger on table1?, so you can delete records only from table1 and then let the trigger delete stuff from table2 and table3.
Something like:
CREATE TRIGGER [dbo].[T_deleteT2_t3]
ON [dbo].[Table1]
AFTER DELETE
AS
BEGIN
delete from table2 where id = select id from deleted
delete from table3 where id = select id from deleted
END
1
It's a valid answer, but I personally don't like the use of triggers in this scenario, triggers in general i'm not a fan of due to their 'hidden' nature.
– Jamie Rees
Nov 23 '18 at 12:05
What if you delete 100 rows from this table?
– Sami
Nov 23 '18 at 12:05
3
@JamieRees How that can be a valid answer while it hasid = select id from deleted
?
– Sami
Nov 23 '18 at 12:06
@Sami I overlooked that, good point
– Jamie Rees
Nov 23 '18 at 12:06
@Sami Its preudocode just to know if the answer could fit him
– picklerick
Nov 23 '18 at 12:08
add a comment |
can you use a trigger on table1?, so you can delete records only from table1 and then let the trigger delete stuff from table2 and table3.
Something like:
CREATE TRIGGER [dbo].[T_deleteT2_t3]
ON [dbo].[Table1]
AFTER DELETE
AS
BEGIN
delete from table2 where id = select id from deleted
delete from table3 where id = select id from deleted
END
can you use a trigger on table1?, so you can delete records only from table1 and then let the trigger delete stuff from table2 and table3.
Something like:
CREATE TRIGGER [dbo].[T_deleteT2_t3]
ON [dbo].[Table1]
AFTER DELETE
AS
BEGIN
delete from table2 where id = select id from deleted
delete from table3 where id = select id from deleted
END
answered Nov 23 '18 at 12:02
picklerickpicklerick
32918
32918
1
It's a valid answer, but I personally don't like the use of triggers in this scenario, triggers in general i'm not a fan of due to their 'hidden' nature.
– Jamie Rees
Nov 23 '18 at 12:05
What if you delete 100 rows from this table?
– Sami
Nov 23 '18 at 12:05
3
@JamieRees How that can be a valid answer while it hasid = select id from deleted
?
– Sami
Nov 23 '18 at 12:06
@Sami I overlooked that, good point
– Jamie Rees
Nov 23 '18 at 12:06
@Sami Its preudocode just to know if the answer could fit him
– picklerick
Nov 23 '18 at 12:08
add a comment |
1
It's a valid answer, but I personally don't like the use of triggers in this scenario, triggers in general i'm not a fan of due to their 'hidden' nature.
– Jamie Rees
Nov 23 '18 at 12:05
What if you delete 100 rows from this table?
– Sami
Nov 23 '18 at 12:05
3
@JamieRees How that can be a valid answer while it hasid = select id from deleted
?
– Sami
Nov 23 '18 at 12:06
@Sami I overlooked that, good point
– Jamie Rees
Nov 23 '18 at 12:06
@Sami Its preudocode just to know if the answer could fit him
– picklerick
Nov 23 '18 at 12:08
1
1
It's a valid answer, but I personally don't like the use of triggers in this scenario, triggers in general i'm not a fan of due to their 'hidden' nature.
– Jamie Rees
Nov 23 '18 at 12:05
It's a valid answer, but I personally don't like the use of triggers in this scenario, triggers in general i'm not a fan of due to their 'hidden' nature.
– Jamie Rees
Nov 23 '18 at 12:05
What if you delete 100 rows from this table?
– Sami
Nov 23 '18 at 12:05
What if you delete 100 rows from this table?
– Sami
Nov 23 '18 at 12:05
3
3
@JamieRees How that can be a valid answer while it has
id = select id from deleted
?– Sami
Nov 23 '18 at 12:06
@JamieRees How that can be a valid answer while it has
id = select id from deleted
?– Sami
Nov 23 '18 at 12:06
@Sami I overlooked that, good point
– Jamie Rees
Nov 23 '18 at 12:06
@Sami I overlooked that, good point
– Jamie Rees
Nov 23 '18 at 12:06
@Sami Its preudocode just to know if the answer could fit him
– picklerick
Nov 23 '18 at 12:08
@Sami Its preudocode just to know if the answer could fit him
– picklerick
Nov 23 '18 at 12:08
add a comment |
Here is another way using OUTPUT
clause
CREATE TABLE T1( ID INT);
CREATE TABLE T2( ID INT);
CREATE TABLE T3( ID INT);
INSERT INTO T1 VALUES (1), (2);
INSERT INTO T2 VALUES (1), (3);
INSERT INTO T3 VALUES (1), (4);
BEGIN TRAN;
DECLARE @Out TABLE (IDs INT);
DELETE T1
OUTPUT DELETED.ID INTO @Out
FROM T1 INNER JOIN T2 ON T1.ID = T2.ID;
DELETE T2
FROM T2 INNER JOIN @Out O ON T2.ID = O.IDs;
DELETE T3
FROM T3 INNER JOIN @Out O ON T3.ID = O.IDs;
COMMIT TRAN;
SELECT *
FROM T1;
SELECT *
FROM T2;
SELECT *
FROM T3;
Demo
add a comment |
Here is another way using OUTPUT
clause
CREATE TABLE T1( ID INT);
CREATE TABLE T2( ID INT);
CREATE TABLE T3( ID INT);
INSERT INTO T1 VALUES (1), (2);
INSERT INTO T2 VALUES (1), (3);
INSERT INTO T3 VALUES (1), (4);
BEGIN TRAN;
DECLARE @Out TABLE (IDs INT);
DELETE T1
OUTPUT DELETED.ID INTO @Out
FROM T1 INNER JOIN T2 ON T1.ID = T2.ID;
DELETE T2
FROM T2 INNER JOIN @Out O ON T2.ID = O.IDs;
DELETE T3
FROM T3 INNER JOIN @Out O ON T3.ID = O.IDs;
COMMIT TRAN;
SELECT *
FROM T1;
SELECT *
FROM T2;
SELECT *
FROM T3;
Demo
add a comment |
Here is another way using OUTPUT
clause
CREATE TABLE T1( ID INT);
CREATE TABLE T2( ID INT);
CREATE TABLE T3( ID INT);
INSERT INTO T1 VALUES (1), (2);
INSERT INTO T2 VALUES (1), (3);
INSERT INTO T3 VALUES (1), (4);
BEGIN TRAN;
DECLARE @Out TABLE (IDs INT);
DELETE T1
OUTPUT DELETED.ID INTO @Out
FROM T1 INNER JOIN T2 ON T1.ID = T2.ID;
DELETE T2
FROM T2 INNER JOIN @Out O ON T2.ID = O.IDs;
DELETE T3
FROM T3 INNER JOIN @Out O ON T3.ID = O.IDs;
COMMIT TRAN;
SELECT *
FROM T1;
SELECT *
FROM T2;
SELECT *
FROM T3;
Demo
Here is another way using OUTPUT
clause
CREATE TABLE T1( ID INT);
CREATE TABLE T2( ID INT);
CREATE TABLE T3( ID INT);
INSERT INTO T1 VALUES (1), (2);
INSERT INTO T2 VALUES (1), (3);
INSERT INTO T3 VALUES (1), (4);
BEGIN TRAN;
DECLARE @Out TABLE (IDs INT);
DELETE T1
OUTPUT DELETED.ID INTO @Out
FROM T1 INNER JOIN T2 ON T1.ID = T2.ID;
DELETE T2
FROM T2 INNER JOIN @Out O ON T2.ID = O.IDs;
DELETE T3
FROM T3 INNER JOIN @Out O ON T3.ID = O.IDs;
COMMIT TRAN;
SELECT *
FROM T1;
SELECT *
FROM T2;
SELECT *
FROM T3;
Demo
answered Nov 23 '18 at 12:20
SamiSami
9,40331244
9,40331244
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53446226%2fdeleting-from-multiple-tables-using-a-left-join-in-one-query%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Remove the first 'FROM' clause and try
– Abdul Rasheed
Nov 23 '18 at 11:54
2
SQL Server only allows you to delete from one table at a time. Perhaps you want cascading foreign key constraints.
– Gordon Linoff
Nov 23 '18 at 11:54
@AbdulRasheed that doesn't work, I tried that
– Jamie Rees
Nov 23 '18 at 11:55
@GordonLinoff let's say this is quite an old database and db integrity wasn't that thought out of at the time...
– Jamie Rees
Nov 23 '18 at 11:56
First you need to remove the first from, 2nd SQL Server allow to delete just from one table, 3rd you can use
OUTPUT
clause to delete from 3 tables, or even so byON DELETE CASCADE
– Sami
Nov 23 '18 at 11:56