Re: Can some explain this to me?



The behavior is expected, if very painful in this case.

A subquery can reference any column in any table in the FROM clause of
the subquery, but it can also reference any column in any table from
the outer query. The reference to pers_companyid in the subquery was
resolved against the outer query table person. Had it been a column
in the subquery table company it would have acted as you intended.

The way to avoid this is to always qualify all column references in a
subquery. For a query such as this it would also make sense to
execute the subquery alone before executing the DELETE. It is also a
good idea to run a query first that establishes how many rows are to
be deleted and check that the number makes sense. Then run the DELETE
inside a transaction and if the count is different roll the
transaction back.

Roy Harvey
Beacon Falls, CT

On Thu, 25 Oct 2007 11:27:23 -0000, davidaustinarcher@xxxxxxxxx wrote:

Hello,

I run the following query on a database (SQL 2005):

delete from person where pers_companyid in (select pers_companyid from
company where comp_expiry is not null)

I wanted to delete all people associated with a company which had an
expiry date. The mistake I made was that the column pers_companyid
does not exist. It should have been comp_companyid.

However, the query ran anyway and deleted all records from my person
table? If I run the subquery on its own then it doesn't run as the
column is missing.

Shouldn't I have got an error running this query?

Thanks,

David
.



Relevant Pages

  • Re: asterisk in select_list in queries...
    ... subquery or other subqueries that don't return data and aren't referenced by ... Listing only the required columns also increases the opportunities for SQL ... Server to optimize your query by making use of indexes. ... that needs to reference the new column. ...
    (microsoft.public.sqlserver.server)
  • Re: Querry not given right results
    ... the outer query, not on the inner query filtering PART#. ... >>Let's concentrate on the subquery starting in line 4. ... within that subquery you reference the ... >instance of TABLE1 ...
    (microsoft.public.access.queries)
  • Re: Jet SQL and Virtual tables/subqueries qiestions
    ... subquery, but it's curious... ... repeated trips through the Query Editor's "graphical" side did ... The Jet Expression Evaluator (what functions & stuff are actually ... within brackets within the query. ...
    (microsoft.public.access.queries)
  • Re: Querry not given right results
    ... Let's concentrate on the subquery starting in line 4. ... within that subquery you reference the instance of TABLE1 ... instance of TABLE1 in the inner query. ...
    (microsoft.public.access.queries)
  • RE: Display most recent comment in report
    ... There are parentheses around the subquery! ... I'm not sure just what you are doing, but the SQL statement is a query; ... FROM Projects INNER JOIN [Communications Log] AS CL1 ... NoteDate and Notes as their ControlSource properties. ...
    (microsoft.public.access.gettingstarted)