Connecting Tech Pros Worldwide Help | Site Map

Invalid Udate SQL statement DOES NOT cause error... Does anyone know why??

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 20th, 2005, 03:31 AM
Silvio
Guest
 
Posts: n/a
Default Invalid Udate SQL statement DOES NOT cause error... Does anyone know why??

Here's my update statement:

UPDATE Item1
SET reviewloop = 1, currentreviewstate=5
WHERE itemid in
(SELECT itemid FROM Item2)

The thing is: the table Item2 DOES NOT HAVE a field called itemid.
So, I should receive an error, right? Not so.Instead, every single
record in Item1 was updated.

Does anyone know why SQL Serverr does not trown an error???

Thanks guys,

-Silvio Souza

  #2  
Old July 20th, 2005, 03:31 AM
no spam
Guest
 
Posts: n/a
Default Re: Invalid Udate SQL statement DOES NOT cause error... Does anyone know why??

Because the sub query can reference fields from the update. itemid in this
case will be retrieved from Item1.


  #3  
Old July 20th, 2005, 03:31 AM
David Portas
Guest
 
Posts: n/a
Default Re: Invalid Udate SQL statement DOES NOT cause error... Does anyone know why??

The rule for subqueries is that a column name that can't be resolved to
column within the subquery is assumed to reference a column in the outer
query. If in doubt, use the two-part column name including the table
name/alias.

--
David Portas
SQL Server MVP
--


  #4  
Old July 20th, 2005, 03:31 AM
Philip Yale
Guest
 
Posts: n/a
Default Re: Invalid Udate SQL statement DOES NOT cause error... Does anyone know why??

"no spam" <chuck@sheckmedia.com> wrote in message news:<vCWhc.71068$Lh2.5553@bignews1.bellsouth.net> ...[color=blue]
> Because the sub query can reference fields from the update. itemid in this
> case will be retrieved from Item1.[/color]

I don't think so. SQL certainly doesn't say to itself "Since I can't
find that value in Item2 I'll assume that they must mean the value in
Item1" - that would be catastrophic.

I've just tried this myself, and whilst it didn't give any error, it
didn't update any rows in Item1 either. This makes sense, because
the subquery is simply evaluating to FALSE, so 0 rows are updated in
the main query.
  #5  
Old July 20th, 2005, 03:31 AM
David Portas
Guest
 
Posts: n/a
Default Re: Invalid Udate SQL statement DOES NOT cause error... Does anyone know why??

> I don't think so. SQL certainly doesn't say to itself "Since I can't[color=blue]
> find that value in Item2 I'll assume that they must mean the value in
> Item1" - that would be catastrophic.[/color]

The problem isn't to do with *values* it's to do with resolution of *column
names*. Substitute the word "column" for "value" and your statement
describes exactly what SQL does.

Assuming the column Itemid doesn't exist in Item2, the UPDATE statement you
posted is equivalent to:

UPDATE Item1
SET reviewloop = 1, currentreviewstate=5
WHERE Item1.itemid IN
(SELECT Item1.itemid FROM Item2)

As long as there is at least one row in Item2, every row in Item1 should get
updated.

--
David Portas
SQL Server MVP
--


  #6  
Old July 20th, 2005, 03:31 AM
KA
Guest
 
Posts: n/a
Default Re: Invalid Udate SQL statement DOES NOT cause error... Does anyone know why??

Any field reference in a sub query will always look for the field internally
first and if not found it will look in the outer query. The reason for this
behaviour is that the sub query can use values from the outer query as
selection criteria, in case statements etc.

This is not a bug, it is by design. By always using table qualifiers in all
sql it will never cause a problem even if the developer mistypes a field
name.
Sloppy SQL (without proper table qualifiers etc) may behave funny as in the
example provided by the OP.

Also, if you look at the execution plan for this and similar queries it will
be more clear why. The optimizer usually turn sub queries like this into
joins.


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.