O'Reilly logo

Microsoft® SQL Server 2012 Unleashed by Alex T. Silverstein, Chris Gallelli, Paul T. Bertucci, Ray Rankins

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Variable Assignment in DECLARE Statements

In SQL Server 2012, you can set a variable’s initial value at the same time you declare it. For example, the following line of code declares a variable named @ctr of type int and sets its value to 100:

DECLARE @ctr int = 100

Previously, this functionality was possible only with stored procedure parameters. Assigning an initial value to a variable required a separate SET or SELECT statement. This new syntax simply streamlines the process of assigning an initial value to a variable. The value specified can be a constant or a constant expression, as in the following:

DECLARE @start_time datetime = getdate()

You can even assign the initial value via a subquery, as long as the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required