SQL SERVER – 2008 – Inline Variable Assignment

I loved this feature. I have always wanted this feature to be present in SQL Server. Last time when I met developers from Microsoft SQL Server, I had talked about this feature. I think this feature saves some time but make the code more readable. Inline variable assignment lets you declare a variable and set its value in one line.


---- SQL Server 2005 Way
DECLARE @MyVar INT
SET @MyVar = 5
SELECT @MyVar AS TestVar
GO
---- SQL Server 2008 Way
DECLARE @MyVar INT = 5
SELECT @MyVar AS TestVar
GO

SQL Server 2005 query window.

Published by Pinal Dave on SQLAuthority. More of my work at pinaldave.com.

SQL Scripts
Previous Post
SQL SERVER – Order Of Column In Index
Next Post
SQL SERVER – 2008 – High Availability – Hot Add CPU

Related Posts

4 Comments. Leave new

  • This will be a wonderful feature to have

    now when we will upgrade to SQL 2008… that’s another story

    Reply
  • sumit sharma
    June 25, 2008 3:00 pm

    It is a nice feature of sqlserver2008
    thanks..

    Reply
  • Nava Raj Bogatee
    January 22, 2009 11:31 am

    Please, help me in the following.

    Say, I have 2 Sql Server databases and I sometimes need to pull data from both of them.

    SELECT a.* FROM LocalTable a
    INNER JOIN
    \\ip address\servername\dbname.remote_table b ON a.primarykey=b.somekey

    Is there any way to fill the lengthy name of my remote server into a variable and use @var.remote_table in the code shown above? It often creates problem if ip address or servername changes and also makes the code look nasty.

    Please help me to get rid of this problem out.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.