Hans:
I think the problem was that Matilda was trying to preview the rows to be
deleted as a datasheet. A DELETE statement will execute happily without any
columns being specified, but if you try an switch to datasheet view before
executing the query the error which Matilda experienced will be raised.
Adding the asterisk enables it to be viewed as a datasheet before executing
the statement.
Its generally more efficient to use the EXISTS predicate in cases like this:
DELETE *
FROM T2
WHERE EXISTS
(SELECT * FROM T1
WHERE T1.ItemID = T2.ItemID);
Both will work when looking for matches, but when looking for non-matches
the latter is to be preferred because of the potential problem with Nulls.
NOT IN(SELECT ItemID FROM T1) will not evaluate to True if there is a Null at
the ItemID column position in any row in T1. This is because it’s the
equivalent of a series of Boolean AND operations so if a row in T2 has a
value 'y' in the ItemID column and T1 has values 'x', 'z' and a Null in its
rows then the expression equates to 'y'<>'x' AND 'y'<>'z' AND 'y' <> NULL.
Now because comparison with Null always results in a Null, not a True or
False the whole expression will not evaluate to True as an AND operation
requires all to evaluate to True. When looking for matches Nulls don't
matter as, if we assume that T1 also contains a 'y', it equates to 'y'='x' OR
'y' = 'y' OR 'y' = 'z' OR 'y' = NULL. As the second of these evaluates to
True the fact that the last evaluates to Null is irrelevant as in an OR
operation only one has to evaluate to True. Mathematically this can be
demonstrated more conclusively by applying DeMorgan's Laws, but we don't need
to go that far here.
So if one wished to delete non-matches rather than matches the following
would be best:
DELETE *
FROM T2
WHERE NOT EXISTS
(SELECT * FROM T1
WHERE T1.ItemID = T2.ItemID);
Ken Sheridan
Stafford, England
"Hans Up" wrote:
> Matilda wrote:
> > Thanks Hans, that did it!!
>
> What did it, Matilda? The * character? Wow.
>
> Anyway, glad you finally found joy. :-)
>
> Cheers,
> Hans
>
|