|
|
|
Right there with Babe
      
Group: General Forum Members
Last Login: Tuesday, April 02, 2013 8:43 PM
Points: 729,
Visits: 400
|
|
| Comments posted to this topic are about the item UPDATE
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: Yesterday @ 10:41 AM
Points: 3,788,
Visits: 5,538
|
|
I thought this was a good question because I've run across a number of people who didn't realize it was possible, but the example troubles me. It just won't work. I almost answered no, thinking it was a trick question. The example really needs to be cleaned up.
update table set a1.coulmn=a2.column< from table1 as a1, table2 as a2 where a1.colum1 = a2.column2
First, just saying UPDATE TABLE will throw an error. It should say UPDATE [TABLE], unless it is a typo. I am assuming it's a typo, just like the typos in spelling "column" and the extra < at the end of one line. If not, it reads as if there were three tables: "table", "table1" and "table2"... which won't work either.
We are supposed to be updating the table specified in the first line, but instead the code tries to set a value of a column that was aliased in the FROM (TABLE1 as A1). I've never seen that format so I cleaned it up at little and tested it. It throws the following error:
Msg 4104, Level 16, State 1, Line 33 The multi-part identifier "a1.val" could not be bound.
The only columns which can be updated are those belonging to the table which is the subject of the UPDATE. That table's name is specified immediately after the word UPDATE, and not necessarily after the FROM. Since only one target table can be the target, the names of the target columns do not need to be qualified, although the source column should be.
Two correct formats for using FROM with an UPDATE are found in the examples below.
;with cte (id, val) as ( select 1,'A' union all select 2,'B' union all select 3,'C' ) select * into [table1] from cte ;with cte (id, val) as ( select 1,'X' union all select 2,'Y' union all select 3,'Z' ) select * into [table2] from cte
select 'Table1',* from [table1] select 'Table2',* from [table2]
select 'NOW THE UPDATE HAPPENS' as [GASP!!!]
------------------------------------------------------------------ ----------- proper format for using a FROM clause in an update ------------------------------------------------------------------ update table1 set val=a2.val from table1 a1 join table2 a2 on a1.id = a2.id
----------- (yes, i used a join instead of tablelist/where) ------------------------------------------------------------------
select 'Table1',* from [table1]
drop table table1 drop table table2
Alternatively this will work as well
update table1 set val=a2.val from table2 a2 where table1.id = a2.id
__________________________________________________
Against stupidity the gods themselves contend in vain. -- Friedrich Schiller Stop, children, what's that sound? -- Stephen Stills
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 5:51 AM
Points: 360,
Visits: 1,260
|
|
| I extensively used this way of updating while working on SQL 2000. Now I prefer to use a CTE or, if dealing with SQL 2008, a MERGE statement.
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Friday, January 08, 2010 2:53 AM
Points: 128,
Visits: 10
|
|
The answer for the question is YES. FROM clause can be used in UPDATE query. But the example query given is wrong.
something like this will work update table1 set coulmn=a2.column from table1 as a1, table2 as a2 where a1.colum1 = a2.column2
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Friday, September 21, 2012 3:07 AM
Points: 107,
Visits: 92
|
|
As geervani indicated you can definitely use FROM clause in UPDATE but not in the example given.
I would do:
update a1 Set a1.column= a2.column From table as a1 inner join table2 as a2 On a1.column1=a2.column2
I want my point back 
Victor
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Monday, October 10, 2011 4:37 AM
Points: 1,111,
Visits: 51
|
|
You can also use the FROM in SELECT, INSERTS, DELETE statements, because it is just another way to join tables.
So the (corrected) example code
update a1 set col = a2.col from table1 as a1, table2 as a2 where a1.col = a2.col
will execute the same operation as
update a1 set col = a2.col from table1 as a1 inner join table2 as a2 on a1.col = a2.col
When running operations with a big amount of rows, the second should have the better performance. [EDIT] This is because statement no.1 will first select all rows on both tables and then matches them with the where-clause. Statement no.2 will first match the tables with the join-clause and then select the matching rows. The performance will increase when the clause uses an index (i.e. primary key). [/EDIT]
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Friday, May 03, 2013 3:39 AM
Points: 857,
Visits: 584
|
|
beck.patrick (9/24/2009)
So the (corrected) example code update a1 set col = a2.col from table1 as a1, table2 as a2 where a1.col = a2.col
This will update table1.col to with the value of table2.col when they are already the same... the obvious conclusion is that the columns in the original question (coulmn, colum1, column and column2) are not typos!  Actually, I've worked in places where that would be entirely believable!
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Wednesday, May 25, 2011 2:41 AM
Points: 145,
Visits: 99
|
|
geervani (9/24/2009) The answer for the question is YES. FROM clause can be used in UPDATE query. But the example query given is wrong.
something like this will work update table1 set coulmn=a2.column from table1 as a1, table2 as a2 where a1.colum1 = a2.column2
Agree.
Alternatively you can also use
update table1 set somecolumn=a2.column from table2 as a2 where column1 = a2.column2
point here is if you dont specify alias for table to be updated in the from clause you can use it as table name reference. However, if you do specify alias for table1 in table from clause then you must reference the table with alias.
geervani: In your statement just to keep t-sql cleaner I would use following
update a1 set a1.somecolumn=a2.column from table1 as a1, table2 as a2 where a1.column1 = a2.column2
Bhavesh .NET and SQL Server Blog
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 4:11 PM
Points: 3,390,
Visits: 3,403
|
|
geervani (9/24/2009) The answer for the question is YES. FROM clause can be used in UPDATE query. But the example query given is wrong.
something like this will work update table1 set coulmn=a2.column from table1 as a1, table2 as a2 where a1.colum1 = a2.column2
Yes, I wasn't sure which way to answer since the syntax wasn't quite right.
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: 2 days ago @ 1:42 PM
Points: 2,012,
Visits: 2,839
|
|
A suggestion for future questions. Maybe not all questions need sample code. A simple question such as "Can you use the FROM clause in a T-SQL update statement?" would have been sufficient.
I answered yes and never looked at the code.
|
|
|
|