473,729 Members | 2,353 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Here's my update statement:

UPDATE Item1
SET reviewloop = 1, currentreviewst ate=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
Jul 20 '05 #1
5 2818
Because the sub query can reference fields from the update. itemid in this
case will be retrieved from Item1.
Jul 20 '05 #2
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
--
Jul 20 '05 #3
"no spam" <ch***@sheckmed ia.com> wrote in message news:<vC******* ***********@big news1.bellsouth .net>...
Because the sub query can reference fields from the update. itemid in this
case will be retrieved from Item1.


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.
Jul 20 '05 #4
> 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.


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, currentreviewst ate=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
--
Jul 20 '05 #5
KA
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.
Jul 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
1866
by: Silvio | last post by:
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.
1
5410
by: John Hunter | last post by:
I've recently had a nasty problem with the "Invalid reference to the property Form" error in subforms - nasty because it doesn't seem to consistently happen to all forms which contain the same structure and code. Judging by the forums I've researched, it's not an uncommon error. I'm happy to say I've found a simple solution. OVERVIEW: I have a main form (no record source) which contains two subforms. The subforms are not linked, but...
10
3358
by: jeff regoord | last post by:
A user inputs a float value. The scanf() function gets the value. However, I need to create an error handler with an if else statement saying invalid input if the input is not a number. Does anybody know how I could do this?
5
3432
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS 5), but fails on IIS 6 running on a Win2003 server. The web uses Pages derived from a custom class I wrote (which itself derives from Page) to provide some common functionality. The Page_Load handler the failing webpage starts out like this: ...
5
1730
by: Mariame | last post by:
Hi All, I have a datagrid in the select button i write the following code: Response.Write("<script>window.open('new.aspx', New Page', 'top=120,left=200,width=440,height=340,modal=yes,scrollbars=yes');</script>" ) but it give me this error "Invalid argument." What is wrong in it????????
12
4045
by: bcr07548 | last post by:
I am writing a web site that uses JavaScript to validate certain forms and I seem to be having some trouble. The site uses PHP and for one of the forms, depending on the situation, one of of the text inputs does not exist - the PHP doesn't generate the HTML for it. I wanted to have the validation script check the length of the input if it exists but I knew that I might have trouble if I tried to check the value of the input without first...
1
2723
by: DCC700 | last post by:
After upgrading a web application from VS 2003 to 2005 there is a page where any control event that should cause a postback instead generates an Invalid character error on the page. For example a drop down list is set to AutoPostBack, and for OnSelectedIndexChanged should update the values of other drop down lists. This page works fine in VS 2003, and the other pages with similar controls are posting back fine in 2005. Has anyone else...
1
2320
by: David | last post by:
Hi, I cannot get the following (MS Access) SQL statement working in my asp page, please can anyone help me ? Thanks :-) ------------------------------------------------ <% strQuery = "SELECT Customers.CustomerName, OrderLines.JobNumber, Orders.PONumber, OrderLines.OrderQuantity,
9
4561
by: 200dogz | last post by:
Hi guys, I want to have a button which opens up a new window when pressed. <asp:Button ID="Button1" runat="server" Text="Open new window" /> ... Button1.Attributes.Add("OnClick",
0
8917
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9426
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9281
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9200
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9142
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8148
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6022
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3238
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.