Home > Software design >  Can anyone explain what this script in SQL means?
Can anyone explain what this script in SQL means?

Time:09-14

UPDATE trgt
SET trgt.User = dbo.ScrambleEmail ( @Mode, @Key, trgt.[User] )
FROM [company].[User_Group] trgt

I'm a little confused about this update statement. I understand that it is calling an existing function during the update statement but it's a little different from other update statements. Normally an update statement ends with "where" however this update statement ends with "from". Furthermore "trgt" seems to replace the table name instead

CodePudding user response:

Missing a WHERE clause means the UPDATE query looks at EVERY ROW. This is usually a mistake, but occasionally it is the intended behavior. With a function call like that, it's also possible the function is smart enough to return the original value for a lot of the rows.

The trgt identifier is called an alias, and you should be using aliases in nearly all your queries. They often reduce the amount of code you need to write, help disambiguate between similar tables, help distinguish between instances when you need to reference the same table more than once, and have several other benefits.

CodePudding user response:

In this case you are updating the User column of every row in the table with the output of the stored function call dbo.ScrambleEmail ( @Mode, @Key, trgt.[User] ) .

  • Related