Declare and Assign Value to a Variable in SQL Server
SQL Server 2008 introduced sql variable declaration and inline value assignment for T-SQL developers. This new feature enables SQL programmers to create a new sql variable using DECLARE command and directly assign it to a value using "=" (equal) sign in the same statement.
In-line variable declaration and value assignment enables developers to code less and makes it easier to read the SQL code.
Here I'm adding a few in-line variable declaration and value assignment samples for SQL developers. As you see below, instead of coding two lines of SQL statements, now the same task can be fulfilled in a single statement.
Here is another sample SQL variable declare command and value assignment in a single statement.
One important note for SQL programmers, if you develop applications that will work on different versions of SQL Server, note that in-line variable assignment works with SQL Server 2008 and later versions, not for SQL Server 2005 or SQL Server 2000, etc.