Blog Post

Smooth Compound Operators

,

Ok, so I can’t promise you’ll be smooth by the end of this entry, but I can promise you’ll be compound operating like a champ. In SQL 2008 there is a new feature called .. you guessed it … Compound Operator. What Microsoft means by this is they have implemented functionality that allows you to perform an arithmetic operation and assign the new value to a variable in the same operation.

 

This is extremely useful and will cut down on your code volume and complexity. I had been reading up on these this week and  I adopted some of the examples from Microsoft’s white paper to make them more straightforward for you and me.

 

You can see what I mean below in the examples. Try these out in your code this week and let me know what you think. I think they are great and I am already using them in my code. I guess you could say it makes you code “smoother”. I couldn’t resist. Enjoy the examples below and email me with questions..

 

--Examples --  += (plus equals) 
            DECLARE @price AS MONEY = 10.00; -- Uses new declare and initialize functionality 
            SET @price += 2.00; 
            SELECT @price as Solution , ' This code += sets the variable @price to its current value, 10.00, plus 2.00, resulting in 12.00.' as [Description] 
            GO

 

--Examples --  -=  (minus equals) 
            DECLARE @price AS MONEY = 10.00; -- Uses new declare and initialize functionality 
            SET @price -= 2.00; 
            SELECT @price as Solution , ' This code -= sets the variable @price to its current value, 10.00, minus 2.00, resulting in 8.00.' as [Description] 
            GO

 

--Examples --  *=  (multiplication equals) 
            DECLARE @price AS MONEY = 10.00; -- Uses new declare and initialize functionality 
            SET @price *= 2.00; 
            SELECT @price as Solution , ' This code *= sets the variable @price to its current value, 10.00, multiplied by 2.00, resulting in 20.00.' as [Description] 
            GO

 

--Examples --  /=  (division equals) 
            DECLARE @price AS MONEY = 10.00; -- Uses new declare and initialize functionality 
            SET @price /= 2.00; 
            SELECT @price as Solution , ' This code /= sets the variable @price to its current value, 10.00, divided by 2.00, resulting in 5.00.' as [Description] 
            GO

 

--Examples --  %=  (modulo equals) 
            DECLARE @price AS MONEY = 10.00; -- Uses new declare and initialize functionality 
            SET @price %= 6.00; 
            SELECT @price as Solution , ' This code %= sets the variable @price to its current value, 10.00, mod 6.00, resulting in 4.00.' as [Description] 
            GO

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating