4

The following SQL statement executes fine on my database:

SELECT * FROM tblKPIs AS k 
INNER JOIN tblKeyPointLinks AS l ON k.KPIID = l.KPIID 
INNER JOIN tblKeyPoints AS p ON p.KptID = l.KptID 
INNER JOIN tblHistory AS h ON h.HistoryID = p.HistoryID 
WHERE h.CaseNo = 50043;

Yet the equivalent Delete statement gives an error 'syntax near AS'?

DELETE FROM tblKPIs AS k 
INNER JOIN tblKeyPointLinks AS l ON k.KPIID = l.KPIID 
INNER JOIN tblKeyPoints AS p ON p.KptID = l.KptID 
INNER JOIN tblHistory AS h ON h.HistoryID = p.HistoryID 
WHERE h.CaseNo = 50043;

Can I not use Joins in Delete statements?

If not how do I perform the above Delete?

EDIT

The table tblKeyPointLinks is an intermediate table to establish a many-to-many relationship between tblKPIs and tblKeyPoints. Therefore the SELECT statement returns some of the entries in tblKPIs more than once. Is this why the DELETE statement is having problem perhaps? What is the best way to work around this?

1
  • What version of SQL? The syntax varies. Thy removing the AS k. Commented Jul 30, 2012 at 1:04

3 Answers 3

5

Yes you can JOIN in delete statements:

DELETE k FROM tblKPIs AS k 
INNER JOIN tblKeyPointLinks AS l ON k.KPIID = l.KPIID 
INNER JOIN tblKeyPoints AS p ON p.KptID = l.KptID 
INNER JOIN tblHistory AS h ON h.HistoryID = p.HistoryID 
WHERE h.CaseNo = 50043;
Sign up to request clarification or add additional context in comments.

2 Comments

Also returns the error "The multi-part identifier k.KPIID could not be bound"
I think it's erroring because you have a typo in your example. Is KPIID a column in tblKPIs?
2

You could just do:

DELETE FROM tblKPIs 
  WHERE id in (
    SELECT id 
      FROM tblKPIs AS k 
      INNER JOIN tblKeyPointLinks AS l ON k.KPIID = l.KPIID 
      INNER JOIN tblKeyPoints AS p ON p.KptID = l.KptID 
      INNER JOIN tblHistory AS h ON h.HistoryID = p.HistoryID 
      WHERE h.CaseNo = 50043)

4 Comments

Now returns the error "The multi-part identifier k.KPIID could not be bound"
I think it's erroring because you have a typo in your example. Is KPIID a column in tblKPIs?
i agree with @BrianDishaw. It sounds like you are trying to join on a column that doesn't exist...
No typos. KPIID is the primary key in tblKPIs. Also not in the original post I said the exact same statement works if preceded with a SELECT rather than a DELETE.
2

The successful code was as follows:

DELETE k
FROM tblKPIs k  
     INNER JOIN tblKeyPointLinks l ON k.KPIID = l.KPIID
     INNER JOIN tblKeyPoints p ON p.KptID = l.KptID
     INNER JOIN tblHistory h ON h.HistoryID = p.HistoryID
WHERE h.CaseNo = 50043; 

Apparently DELETE statements don't like the use of the keyword AS By simply omitting the AS keyword the statement works fine.

3 Comments

This is the same as Igor's solution. The only difference is, his contains AS before aliases.
Yes I know - think I say as much - does that really justify marking my solution down?
I'm sorry but I don't know what you are talking about. If you mean that someone downvoted your answer, it wasn't me. Anyway, I can see now that there's no downvote on your answer, so maybe you meant something else.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.