Alternative UPDATE Syntax

  • 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

  • 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

  • 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.

  • 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

  • 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.

  • @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.

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply