Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Data Bases > Microsoft SQL Server > Re: updating mu...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 5 of 5 Topic 11025 of 11517
Post > Topic >>

Re: updating multiple columns with subselect

by --CELKO-- <jcelko212@[EMAIL PROTECTED] > May 12, 2008 at 01:00 PM

The proprietary syntax does not work all the time:

CREATE TABLE Orders
(order_nbr INTEGER NOT NULL PRIMARY KEY,
 some_col DECIMAL (9,2) NOT NULL);

INSERT INTO Orders VALUES (1, 0);
INSERT INTO Orders VALUES (2, 0);
INSERT INTO Orders VALUES (3, 0);

CREATE TABLE OrderDetails
(order_nbr INTEGER NOT NULL,
 sku INTEGER NOT NULL,
 item_price DECIMAL (9,2) NOT NULL,
 PRIMARY KEY(order_nbr, sku),
-- FOREIGN KEY(sku) REFERENCES Products(sku)
 FOREIGN KEY(order_nbr) REFERENCES Orders(order_nbr));

INSERT INTO OrderDetails VALUES (1, 1, 500.00);
INSERT INTO OrderDetails VALUES (1, 2, 205.00);
INSERT INTO OrderDetails VALUES (2, 1, 490.95);
INSERT INTO OrderDetails VALUES (3, 1, 480.00);

SELECT * FROM Orders;

UPDATE Orders
   SET Orders.some_col = OrderDetails.item_price
  FROM Orders
       INNER JOIN
       OrderDetails
       ON Orders.order_nbr = OrderDetails.order_nbr;

  results -- see item #1; last physical value
1	205.00  - where is the $500.00?
2	490.95
3	480.00

--repeat with new physical ordering
DELETE FROM OrderDetails;
DELETE FROM Orders;
DROP INDEX OrderDetails.foobar;

-- index will change the execution plan
CREATE INDEX foobar ON OrderDetails (order_nbr, item_price);

INSERT INTO Orders VALUES (1, 0);
INSERT INTO Orders VALUES (2, 0);
INSERT INTO Orders VALUES (3, 0);

INSERT INTO OrderDetails VALUES (1, 2, 205.00);
INSERT INTO OrderDetails VALUES (1, 1, 500.00);
INSERT INTO OrderDetails VALUES (2, 1, 490.95);
INSERT INTO OrderDetails VALUES (3, 1, 480.00);

UPDATE Orders
   SET Orders.some_col = OrderDetails.item_price
  FROM Orders
       INNER JOIN
       OrderDetails
       ON Orders.order_nbr = OrderDetails.order_nbr;

SELECT * FROM Orders;

Results
1	500.00
2	490.95
3	480.00

What is the first property that you must have in an INDEX?  It cannot
change the results of a statement, only the performance. See the
problem?

This would not have happened with the ANSI syntax. That's the point
that I am trying to make. The ANSI equivalent of the incorrect query
above is

UPDATE Orders --  no alias allowed!
   SET some_col
    = (SELECT item_price
         FROM OrderDetails
        WHERE OrderDetails.order_nbr = Orders.order_nbr)
 WHERE EXISTS
      (SELECT *
         FROM OrderDetails
        WHERE OrderDetails.order_nbr = Orders.order_nbr);

This will of course result in an error, and even the most junior of
junior programmers will eventually figure out (probably by asking a
senior) what's wrong. At that point, either the query is corrected to
match the request, or a note is sent back to management asking for a
clarification of the ambiguity in the request.

Bottom line: UPDATE FROM *can* be safely used - but only if you are
FOREVER certain that no single row in the target table can EVER be
joined to more than one row in the source table(s); FOREVER in the
ENTIRE LIFETIME of the application; FOREVER across all programmers yet
to come.

I like to err on the safe side, I do not bet only an endless stream of
100% perfect programmers.  You correctly guessed the row constructor
syntax that is standard in ANSI/ISO SQL, by the way.
 




 5 Posts in Topic:
updating multiple columns with subselect
"Leif Neland" &  2008-05-08 12:36:13 
Re: updating multiple columns with subselect
"Dan Guzman" &l  2008-05-08 07:42:28 
Re: updating multiple columns with subselect
"Leif Neland" &  2008-05-09 09:52:29 
Re: updating multiple columns with subselect
"Plamen Ratchev"  2008-05-08 09:37:43 
Re: updating multiple columns with subselect
--CELKO-- <jcelko212@[  2008-05-12 13:00:07 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Tue Dec 2 22:08:57 CST 2008.