Wednesday, March 7, 2012

Delete using 2 tables

I need to delete records in 1 table based on matching data in a 2nd table.
Below is my syntax. Is this a good syntax for this?
DELETE dbo.InternalCSIAnswers
FROM dbo.InternalCSIQuestions
WHERE (dbo.InternalCSIAnswers.InternalCSIID =
dbo.InternalCSIQuestions.InternalCSIID)
AND (dbo.InternalCSIAnswers.RepairOrderID = 26981
AND (dbo.InternalCSIQuestions.InternalType = 'E')
DavidI prefer the ANSI DELETE syntax:
DELETE FROM InternalCSIAnswers
WHERE EXISTS
(SELECT *
FROM InternalCSIQuestions AS Q
WHERE Q.InternalCSIID = InternalCSIAnswers.internalcsiid
AND Q.internaltype = 'E')
AND repairorderid = 26981 ;
This has the potential advantage of being standard SQL, unlike the
proprietary Microsoft extension you used (although the Microsoft version
often yields more efficient execution).
David Portas
SQL Server MVP
--|||DELETE dbo.InternalCSIAnswers
FROM dbo.InternalCSIAnswers a
join dbo.InternalCSIQuestions q
on q.InternalCSIID = a.InternalCSIID
WHERE a.RepairOrderID = 26981
AND q.InternalType = 'E'
"David" wrote:

> I need to delete records in 1 table based on matching data in a 2nd table.
> Below is my syntax. Is this a good syntax for this?
> DELETE dbo.InternalCSIAnswers
> FROM dbo.InternalCSIQuestions
> WHERE (dbo.InternalCSIAnswers.InternalCSIID =
> dbo.InternalCSIQuestions.InternalCSIID)
> AND (dbo.InternalCSIAnswers.RepairOrderID = 26981
> AND (dbo.InternalCSIQuestions.InternalType = 'E')
> David
>
>

No comments:

Post a Comment