I'm using this
DELETE TableA
FROM TableA a
INNER JOIN
TableB b on b.Bid = a.Bid
AND [condition]
and @TheTXI way is good as enough but I read answers and comments and I found one things must be answered is using condition in WHERE clause or as join condition. So I decided to test it and write an snippet but didn't find a meaningful difference between them. You can see sql script here and important point is that I preferred to write it as commnet because of this is not exact answer but it is large and can't be put in comments, please pardon me.
Declare @TableA Table
(
aId INT,
aName VARCHAR(50),
bId INT
)
Declare @TableB Table
(
bId INT,
bName VARCHAR(50)
)
Declare @TableC Table
(
cId INT,
cName VARCHAR(50),
dId INT
)
Declare @TableD Table
(
dId INT,
dName VARCHAR(50)
)
DECLARE @StartTime DATETIME;
SELECT @startTime = GETDATE();
DECLARE @i INT;
SET @i = 1;
WHILE @i < 1000000
BEGIN
INSERT INTO @TableB VALUES(@i, 'nameB:' + CONVERT(VARCHAR, @i))
INSERT INTO @TableA VALUES(@i+5, 'nameA:' + CONVERT(VARCHAR, @i+5), @i)
SET @i = @i + 1;
END
SELECT @startTime = GETDATE()
DELETE a
--SELECT *
FROM @TableA a
Inner Join @TableB b
ON a.BId = b.BId
WHERE a.aName LIKE '%5'
SELECT Duration = DATEDIFF(ms,@StartTime,GETDATE())
SET @i = 1;
WHILE @i < 1000000
BEGIN
INSERT INTO @TableD VALUES(@i, 'nameB:' + CONVERT(VARCHAR, @i))
INSERT INTO @TableC VALUES(@i+5, 'nameA:' + CONVERT(VARCHAR, @i+5), @i)
SET @i = @i + 1;
END
SELECT @startTime = GETDATE()
DELETE c
--SELECT *
FROM @TableC c
Inner Join @TableD d
ON c.DId = d.DId
AND c.cName LIKE '%5'
SELECT Duration = DATEDIFF(ms,@StartTime,GETDATE())
If you could get good reason from this script or write another useful, please share. Thanks and hope this help.