Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Multi-Part Identifier Error on Temp Table Where Exists

I’m trying to delete records from #tableA that do not exist in #tableB. Seems pretty straightforward, however, I’m recieving the "multi-part identifier #TABLEA.MODELNO cannot be bound" even though "modelno" column exists in tableA. What am I missing?

DELETE FROM #TABLEA
WHERE EXISTS (SELECT 1 FROM #TABLEB WHERE #TABLEB.MODEL = #TABLEA.MODELNO)

I’d like to avoid using WHERE MODELNO IN() for performance reasons.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Your syntax is off, and the DELETE keyword should be followed by a target. Assuming you alias both tables, the following should work:

DELETE a
FROM #TABLEA a
WHERE NOT EXISTS (
    SELECT 1
    FROM #TABLEB b
    WHERE b.MODEL = a.MODELNO
);

If you wanted to use a join approach, the following anti-join with the help of a CTE should work:

WITH cte AS (
    SELECT a.*
    FROM #TABLEA a
    LEFT JOIN #TABLEB b
        ON b.MODEL = a.MODELNO
    WHERE b.MODEL IS NULL
)

DELETE FROM cte;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading