Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Table alias with UPDATE ?

 
   Database Help (Home) -> Programming RSS
Next:  Getting Data Out of Delimited List  
Author Message
Rav

External


Since: Nov 07, 2007
Posts: 6



(Msg. 1) Posted: Tue May 13, 2008 4:35 pm
Post subject: Table alias with UPDATE ?
Archived from groups: microsoft>public>sqlserver>programming (more info?)

Is there a way to use a table alias with an UPDATE that has no FROM
clause? I'm using SQL 2000. For example:

UPDATE TableLongName
SET myfield3 = 789
WHERE TableLongName.myfield1 = 123 AND TableLongName.myfield2 = 456

works, but isn't as easy to read (or write) as something like:

UPDATE TableLongName T
SET myfield3 = 789
WHERE T.myfield1 = 123 AND T.myfield2 = 456

which signals the error Incorrect syntax near 'T'.
** Posted from http://www.teranews.com **

 >> Stay informed about: Table alias with UPDATE ? 
Back to top
Login to vote
Alex Kuznetsov

External


Since: Jan 10, 2008
Posts: 640



(Msg. 2) Posted: Tue May 13, 2008 4:35 pm
Post subject: Re: Table alias with UPDATE ? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On May 13, 3:35 pm, Rav wrote:
> Is there a way to use a table alias with an UPDATE that has no FROM
> clause? I'm using SQL 2000. For example:
>
> UPDATE TableLongName
> SET myfield3 = 789
> WHERE TableLongName.myfield1 = 123 AND TableLongName.myfield2 = 456
>
> works, but isn't as easy to read (or write) as something like:
>
> UPDATE TableLongName T
> SET myfield3 = 789
> WHERE T.myfield1 = 123 AND T.myfield2 = 456
>
> which signals the error Incorrect syntax near 'T'.
> ** Posted fromhttp://www.teranews.com**

What is the problem that you are trying to solve with an alias?

 >> Stay informed about: Table alias with UPDATE ? 
Back to top
Login to vote
--CELKO--

External


Since: Jan 11, 2008
Posts: 1089



(Msg. 3) Posted: Tue May 13, 2008 4:35 pm
Post subject: Re: Table alias with UPDATE ? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

>> Is there a way to use a table alias with an UPDATE that has no FROM clause? I'm using SQL 2000. <<

You might want to get a book on Standard SQL so you will not confuse
SQL Server's highly proprietary syntax with it. The UPDATE clause
cannot have an alias because an alias is supposed to act as if new
working table has been constructed from the base table. It will
disappear at the end of the query, so updating it would be useless.
This model for aliases is consistent in Standard SQL.

>> For example: .. <<

All you need is

UPDATE TableLongName
SET myfield3 = 789
WHERE myfield1 = 123
AND myfield2 = 456;

Since there is only one table in the scope of the UPDATE clause.
 >> Stay informed about: Table alias with UPDATE ? 
Back to top
Login to vote
Aaron Bertrand [SQL Serve

External


Since: Jan 10, 2008
Posts: 2166



(Msg. 4) Posted: Tue May 13, 2008 4:45 pm
Post subject: Re: Table alias with UPDATE ? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Why do you think you need an alias here? You are affecting one table. The
alias and column prefixes are both superfluous. Imagine:

UPDATE TableLongName
SET myfield3 = 789
WHERE myfield1 = 123 AND myfield2 = 456






"Rav" wrote in message

> Is there a way to use a table alias with an UPDATE that has no FROM
> clause? I'm using SQL 2000. For example:
>
> UPDATE TableLongName
> SET myfield3 = 789
> WHERE TableLongName.myfield1 = 123 AND TableLongName.myfield2 = 456
>
> works, but isn't as easy to read (or write) as something like:
>
> UPDATE TableLongName T
> SET myfield3 = 789
> WHERE T.myfield1 = 123 AND T.myfield2 = 456
>
> which signals the error Incorrect syntax near 'T'.
> ** Posted from http://www.teranews.com **
 >> Stay informed about: Table alias with UPDATE ? 
Back to top
Login to vote
"Roy Harvey

External


Since: Jan 12, 2008
Posts: 593



(Msg. 5) Posted: Tue May 13, 2008 4:54 pm
Post subject: Re: Table alias with UPDATE ? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

UPDATE T
FROM TableLongName T
WHERE T.myfield1 = 123 AND T.myfield2 = 456

Roy Harvey
Beacon Falls, CT

On Tue, 13 May 2008 16:35:47 -0400, Rav wrote:

>Is there a way to use a table alias with an UPDATE that has no FROM
>clause? I'm using SQL 2000. For example:
>
>UPDATE TableLongName
>SET myfield3 = 789
>WHERE TableLongName.myfield1 = 123 AND TableLongName.myfield2 = 456
>
>works, but isn't as easy to read (or write) as something like:
>
>UPDATE TableLongName T
>SET myfield3 = 789
>WHERE T.myfield1 = 123 AND T.myfield2 = 456
>
>which signals the error Incorrect syntax near 'T'.
>** Posted from http://www.teranews.com **
 >> Stay informed about: Table alias with UPDATE ? 
Back to top
Login to vote
Rav

External


Since: Nov 07, 2007
Posts: 6



(Msg. 6) Posted: Tue May 13, 2008 4:58 pm
Post subject: Re: Table alias with UPDATE ? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Aaron Bertrand [SQL Server MVP] wrote:
> Why do you think you need an alias here? You are affecting one table. The
> alias and column prefixes are both superfluous. Imagine:
>
> UPDATE TableLongName
> SET myfield3 = 789
> WHERE myfield1 = 123 AND myfield2 = 456
>
>
>
> "Rav" wrote in message
>
>> Is there a way to use a table alias with an UPDATE that has no FROM
>> clause? I'm using SQL 2000. For example:
>>
>> UPDATE TableLongName
>> SET myfield3 = 789
>> WHERE TableLongName.myfield1 = 123 AND TableLongName.myfield2 = 456
>>
>> works, but isn't as easy to read (or write) as something like:
>>
>> UPDATE TableLongName T
>> SET myfield3 = 789
>> WHERE T.myfield1 = 123 AND T.myfield2 = 456
>>
>> which signals the error Incorrect syntax near 'T'.
>
>

(Sound of whacking myself upside the head.) Of course, you're right.
Thanks.
..
** Posted from http://www.teranews.com **
 >> Stay informed about: Table alias with UPDATE ? 
Back to top
Login to vote
TheSQLGuru

External


Since: Jan 11, 2008
Posts: 579



(Msg. 7) Posted: Tue May 13, 2008 7:25 pm
Post subject: Re: Table alias with UPDATE ? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

DOHHHHHHH!! (said in my best Homer Simpson voice) Smile

--
Kevin G. Boles
Indicium Resources, Inc.
SQL Server MVP
kgboles a earthlink dt net


"Rav" wrote in message

> Aaron Bertrand [SQL Server MVP] wrote:
>> Why do you think you need an alias here? You are affecting one table.
>> The alias and column prefixes are both superfluous. Imagine:
>>
>> UPDATE TableLongName
>> SET myfield3 = 789
>> WHERE myfield1 = 123 AND myfield2 = 456
>>
>>
>>
>> "Rav" wrote in message
>>
>>> Is there a way to use a table alias with an UPDATE that has no FROM
>>> clause? I'm using SQL 2000. For example:
>>>
>>> UPDATE TableLongName
>>> SET myfield3 = 789
>>> WHERE TableLongName.myfield1 = 123 AND TableLongName.myfield2 = 456
>>>
>>> works, but isn't as easy to read (or write) as something like:
>>>
>>> UPDATE TableLongName T
>>> SET myfield3 = 789
>>> WHERE T.myfield1 = 123 AND T.myfield2 = 456
>>>
>>> which signals the error Incorrect syntax near 'T'.
>>
>>
>
> (Sound of whacking myself upside the head.) Of course, you're right.
> Thanks.
> .
> ** Posted from http://www.teranews.com **
 >> Stay informed about: Table alias with UPDATE ? 
Back to top
Login to vote
David Portas

External


Since: Nov 11, 2003
Posts: 854



(Msg. 8) Posted: Tue May 13, 2008 11:18 pm
Post subject: Re: Table alias with UPDATE ? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

"--CELKO--" wrote in message

>>> Is there a way to use a table alias with an UPDATE that has no FROM
>>> clause? I'm using SQL 2000. <<
>
> You might want to get a book on Standard SQL so you will not confuse
> SQL Server's highly proprietary syntax with it. The UPDATE clause
> cannot have an alias because an alias is supposed to act as if new
> working table has been constructed from the base table. It will
> disappear at the end of the query, so updating it would be useless.
> This model for aliases is consistent in Standard SQL.
>

From ISO/IEC 9075-2:2003, 14.11:

UPDATE <target table> [ [ AS ] <correlation name> ]
SET <set clause list>
[ WHERE <search condition> ]

Correlation names are part of the standard SQL UPDATE. It's just that SQL
Server doesn't support them.

--
David Portas
 >> Stay informed about: Table alias with UPDATE ? 
Back to top
Login to vote
ML

External


Since: Jan 15, 2008
Posts: 380



(Msg. 9) Posted: Fri Jan 16, 2009 2:38 am
Post subject: RE: Table alias with UPDATE ? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

What about this:

update t
set
t.Field1 = s.Field1,
t.Field2 = s.Field2,
t.Field3 = s.Field3
from SpecialOwner.VeryLongTableNameSource as s
inner join SpecialOwner.VeryLongTableNameTarget as t
on t.FieldChk = s.FieldChk

This is also valid syntax, but note that UPDATE...FROM is proprietary to
Microsoft SQL Server, and - most importantly - make sure the result set
returned by the JOIN operation returns at most one row from the source table
for each row in the target table.


ML

---
Matija Lah, SQL Server MVP
http://milambda.blogspot.com/
 >> Stay informed about: Table alias with UPDATE ? 
Back to top
Login to vote
Alex Kuznetsov

External


Since: Jan 10, 2008
Posts: 640



(Msg. 10) Posted: Fri Jan 16, 2009 7:58 am
Post subject: Re: Table alias with UPDATE ? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Jan 16, 4:38 am, ML wrote:
> What about this:
>
> update t
> set
>         t.Field1 = s.Field1,    
>         t.Field2 = s.Field2,
>         t.Field3 = s.Field3
> from SpecialOwner.VeryLongTableNameSource as s
>   inner join SpecialOwner.VeryLongTableNameTarget as t
>     on t.FieldChk = s.FieldChk
>
> This is also valid syntax, but note that UPDATE...FROM is proprietary to
> Microsoft SQL Server, and - most importantly - make sure the result set
> returned by the JOIN operation returns at most one row from the source table
> for each row in the target table.

Here is an example of avoiding ambiguities:

http://sqlblog.com/blogs/alexander_kuznetsov/archive/2008/12/08/defens...-databa
 >> Stay informed about: Table alias with UPDATE ? 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
SELECTing Rows to Columns - How to Get Column Alias From T.. - Considering the following query which transposes rows into columns as desired; please notice that the column name aliases are "hard coded" (e.g., AS ABO, AS RHFactor, AS A1, AS A2, etc). Each row in the table Lookup_TransplantParticipantFact...

using a VALUE + a STRING as an ALIAS? - Hi, I'd like to translate this from Access SQL to 'true' SQL: SELECT "http:/mydomain/" & [Image] AS image_url I've tried several things but SQL Server doesn't accept it. How can I add this string to the value of that field? Thank you v...

Way to alias a database? - Hi, I'm using MS Sql 2005. When I run a query from within "MyDatabase" that accesses another database, "OtherDatabase", is there a way I can create an alias, "OtherDatabaseOtherName", to access "OtherDatabase" so...

Using the field alias name in WHERE clause - Hi, Is there anyway to use the field alias name within the WHERE part of a SQL statement in MS SQL Server 2005? Below is an example: Select Table1.Name as FullName, Table1.Address as Address from Table1 Where FullName like '%test%' What..

using kind of alias in sqlplus - hi all. Can someone please help me on the following. Example query: Tabel1 contains a lot of items. TABLE1 ITEM VARCHAR2(30), FAM1 VARCHAR2(10), FAM2 VARCHAR2(10), FAM3 VARCHAR2(10), FAM4 VARCHAR2(10) Table contains a lot of records with....
   Database Help (Home) -> Programming All times are: Pacific Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]