A little trick you can use with an update or insert is an automatic assignment.
For example at times I’ve had to update all rows in a (temporay) table
with an consecutive number and a fast way of doing this is utilizing
this technique:

DECLARE @I INT

SET @I = 0

UPDATE Table_1

SET @I = MyID = @I + 1

This example instantiates an interger (I) to the value 0. Then I
update a table where I set an ID column to the value of I + 1, but at
the same time I set my I variable to the value of MyID. This means that
the first row will get the value 1 into MyID and the second will get 2
and so forth.

And of course this example is very simple, and in such a case one
might as well just use identity. However because you can make any
calculations you want, and because it does not have to be an integer
data type – you can use this for various situations.

It just goes to show that T-SQL syntax allows for playing around.

One Response to “UPDATE/INSERT syntax trick”

  1. Say thank you for write about incredibly very good informations. Your net is great.I am impressed by the details that you’ve on this blog

Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>