Wednesday, February 13, 2008

SQL Server 2008 - Assignment operators

For instance, while looping when we need to increment counter, in earlier version we used to set the the counter=Counter+1 kind of code. But new assignment operator will make assignment more simple (may be less readable). This may be useful in Join

SQL 2005/ 2000 way of Assigning .

Declare @Counter int
Set @Counter =1
While @Counter <10
Begin
Print @Counter
Set @Counter =@Counter +1
End

SQL 2008 way of Assigning .


Declare @Counter int=1
While @Counter <10
Begin
Print @Counter
Set @Counter +=1
End

Basically, in earlier versions, equal sign (=) was the only Transact-SQL assignment operator. Now in 2008 you have += , -=, *=, /=

No comments: