SQL Server 2008 Inline variable initialization and Compound assignment

By:   |   Updated: 2007-10-15   |   Comments (1)   |   Related: More > T-SQL

In Sql 2008 (Katmai), a couple of the 'smaller' features that are currently in the latest CTP include inline variable initialization and compound assignment (something you App Dev folks have had for years). So, the following types of code now work in Sql 2008:


  declare @d datetime = getdate(),
    @i int = 1,
    @s varchar(100) = 'test';
 
  -- Show the values...
  select @d, @i, @s
 
  -- Increment i, append to s...
  select @i += 1, @s += 'ing';
 
  -- Show the new values...
  select @i, @s;

These operators also work inline with DML statements and columns...for example, a simple UPDATE statement in a table called 'testTable' with a column called 'testColumn' where you wanted to increment the value of testColumn by 1 could be:

  update testTable set testColumn += 1;
 
Also would work with other columns...if another column called testColumn2 existed:

  update testTable set testColumn += testColumn2;
 

I'll be posting other tidbits in Sql 2008 ongoing...enjoy!

Chad Boyd ~~~ This posting is provided "AS IS" with no warranties, and confers no rights. Use of any included script samples are subject to the terms specified at http://www.mssqltips.com/disclaimer.asp and http://www.mssqltips.com/copyright.asp.



Last Updated: 2007-10-15


get scripts

next tip button



About the author
MSSQLTips author Chad Boyd Chad Boyd is an Architect, Administrator and Developer with technologies such as SQL Server, .NET, and Windows Server.

View all my tips




Post a comment or let the author know this tip helped.

All comments are reviewed, so stay on subject or we may delete your comment. Note: your email address is not published. Required fields are marked with an asterisk (*).

*Name
*Email
Email me updates

Signup for our newsletter

I agree by submitting my data to receive communications, account updates and/or special offers about SQL Server from MSSQLTips and/or its Sponsors. I have read the privacy statement and understand I may unsubscribe at any time.





Thursday, February 18, 2016 - 7:49:41 PM - manu Back To Top

Please share such tidbits for latest versions. I really like these and is thankful to you for sharing it here.



download

























get free sql tips

I agree by submitting my data to receive communications, account updates and/or special offers about SQL Server from MSSQLTips and/or its Sponsors. I have read the privacy statement and understand I may unsubscribe at any time.



Learn more about SQL Server tools