Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Alternative UPDATE Syntax Expand / Collapse
Author
Message
Posted Monday, April 15, 2013 8:11 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, April 22, 2013 5:20 AM
Points: 3, Visits: 26
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 :)
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
Post #1442320
Posted Monday, April 15, 2013 8:32 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 1:43 AM
Points: 6,890, Visits: 14,254
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
Post #1442330
Posted Tuesday, April 16, 2013 1:36 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, April 22, 2013 5:20 AM
Points: 3, Visits: 26
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.
Post #1442624
Posted Tuesday, April 16, 2013 1:44 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 1:43 AM
Points: 6,890, Visits: 14,254
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
Post #1442629
Posted Tuesday, April 16, 2013 5:14 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Wednesday, April 24, 2013 7:40 AM
Points: 400, 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.
Post #1442689
Posted Tuesday, April 16, 2013 8:07 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, April 22, 2013 5:20 AM
Points: 3, Visits: 26
@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.
Post #1442760
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse