SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Alternative UPDATE Syntax


Alternative UPDATE Syntax

Author
Message
directdelphi
directdelphi
Valued Member
Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)

Group: General Forum Members
Points: 63 Visits: 28
Hi all,

I have a question about the UPDATE syntax.

There are the following - fictive - tables:

Table ParamValues:

PV_ChangeDate PV_ChangeUser PV_ID PV_Item PV_PK_ID PV_IntValue
----------------------- ------------- ----------- -------------------------------------------------- ---------- -----------
2013-04-15 16:00:15.477 Peter 1 wall 1 900
2013-01-01 00:00:00.000 Peter 2 house 2 200
2013-01-02 00:00:00.000 Doris 3 lantern 1 100
2013-04-15 16:00:56.130 Peter 4 wall 2 70



Table ParamKeys:

PK_ID PK_KeyName PK_Description
----------- -------------------------------------------------- --------------------------------------------------
1 height The Height
2 width The Width



Usually, I use the following syntax - a example:

-- changing the IntValue...
UPDATE ParamValues
SET PV_ChangeDate = GETDATE(),
PV_ChangeUser = 'Peter',
PV_IntValue = 1000
WHERE PV_ID = 123;



Now, I have to use an INNER JOIN. I've read about an alternative syntax (UPDATE ... SET ... FROM ... WHERE) where an alias for the tables is to be used.
For example:

-- changing the height of the wall to 1000 Smile
UPDATE pv
SET pv.PV_ChangeDate = GETDATE(),
pv.PV_ChangeUser = 'Peter',
pv.PV_IntValue = 1000
FROM ParamValues pv
INNER JOIN ParamKeys pk ON pk.PK_ID = pv.PV_PK_ID AND pk.PK_KeyName = 'height'
WHERE pv.PV_Item = 'wall'



But... in my situation I would like to (have to) avoid the alias "pv" in the UPDATE syntax. I tried the following, and it worked for me. But I never read or heard about it. So I don't know if it is an "official allowed" syntax:

UPDATE ParamValues
SET PV_ChangeDate = GETDATE(),
PV_ChangeUser = 'Peter',
PV_IntValue = 1000
FROM ParamValues
INNER JOIN ParamKeys ON PK_ID = PV_PK_ID AND PK_KeyName = 'height'
WHERE PV_Item = 'Wall'



Is it secure to use this last syntax?

Many thanks in advance for you replies!

Greg
ChrisM@Work
ChrisM@Work
SSC Guru
SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)

Group: General Forum Members
Points: 65912 Visits: 20214
Many folks stick to this syntax:

UPDATE pv -- alias of first table listed in FROM clause
SET
PV_ChangeDate = GETDATE(),
PV_ChangeUser = 'Peter',
PV_IntValue = 1000
FROM ParamValues pv -- put the UPDATE target here
INNER JOIN ParamKeys pk -- NOT here
ON pk.PK_ID = pv.PV_PK_ID AND pk.PK_KeyName = 'height'
WHERE pv.PV_Item = 'wall'



Some have reported problems with query layouts different to this - Jeff Moden IIRC recalls a simple UPDATE FROM which slammed the server to the wall for hours on end, which ran in seconds when rewritten as above. I'm not sure if I've observed this or not but I've never had issues with this syntax.

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
directdelphi
directdelphi
Valued Member
Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)

Group: General Forum Members
Points: 63 Visits: 28
Thanks, sure.
The only difference between your SQL and my last SQL is that yours uses aliases and mine not.
My question is if the aliases are mandatory (even if it works without them) or if I can drop them.
ChrisM@Work
ChrisM@Work
SSC Guru
SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)

Group: General Forum Members
Points: 65912 Visits: 20214
Have a look at the query shown in the first post of this thread. There are no aliases on the output columns so we can't tell which columns come from which tables. We have to ask or guess.
Although not always essential for SQL Server, the aliases are information for anyone reading the query. Why would you want to deliberately withhold information?

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Pothiraj R
Pothiraj R
SSC-Addicted
SSC-Addicted (418 reputation)SSC-Addicted (418 reputation)SSC-Addicted (418 reputation)SSC-Addicted (418 reputation)SSC-Addicted (418 reputation)SSC-Addicted (418 reputation)SSC-Addicted (418 reputation)SSC-Addicted (418 reputation)

Group: General Forum Members
Points: 418 Visits: 38
Alias names are optional. The main purpose of using table alias is better readablity. As per your scanario, the prefix of column names will help us to understand the table name. So it is not required.
directdelphi
directdelphi
Valued Member
Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)

Group: General Forum Members
Points: 63 Visits: 28
@ChrisM@Work: In my special case, I would like to deliberately avoid the aliases because I have a Delphi procedure which executes an SQL Command over ADO. This procedure has a parameter which a part (usually the WHERE clause) of the SQL Command can be passed with. It also has a parameter to pass the table name.


procedure MyProc(datChangeDate, strChangeUser, strTableName, strSqlWhere);



Within the procedure, I want to build the complete Command Text like this:


// [...]
// Every table has the columns "ChangeUser" and "ChangeDate"...
AdoCommand.CommandText := 'UPDATE ' + strTableName +
' SET ChangeUser = ' + QuotedStr(strChangeUser) + ', ' +
' ChangeDate = ' + QuotedStr(FormatDateTime('dd.mm.yyyy hh:nn:ss', datChangeDate) + ', ' +
' FROM ' + strTableName +
' ' + strSqlWhere;
// yes, I should better do this with parameters... but doesn't matter here.
AdoCommand.Execute;
// [...]



The reason why I want to use this syntax AND avoid aliases is that the parameter strSqlWhere has to be able to take an INNER JOIN, which perhaps could contain aliases itself. And I want to avoid a situation in which the procedure uses the same name for an alias as the one who calls the procedure does.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search