Enhancement in variable declaration - SQL Server 2008

June 24, 2008

Enhanced variable declaration, one of the many enhancements that Microsoft added to SQL Server 2008 allows you to declare and initialize a value at the same time, similar to other languages. For some reason, this enhancement was never made to the actual variable declaration.

In previous versions, we could declare a parameter in a stored procedure and assign a default value. However, we could never declare a variable and assign value at the same time. All we could do was declare the variable first and assign it later.

This article illustrates how to declare and assign the values for a variable.

Though it is a very small change added to SQL Server 2008, it makes a lot of difference when we declare multiple variables.

In SQL Server 2000 and SQL Server 2005, when executing the following declaration and assignment returns an error message [Refer fig 1.0 and 1.1]

select @@version
go
declare @firstname varchar(100) = 'Derick'
declare @lastname varchar(100) = 'Scott'
declare @salary money = 50000
declare @bonus money = @salary*.10
declare @address varchar(100) = '90, Essex st, Hackensack, NJ'
select @firstname as Firstname
select @Lastname as Lastname
select @Salary as Salary
select @bonus as Bonus
select @address as Address
go

Result in SQL Server 2000 Machine

-----------------------------------------------------------------------
Microsoft SQL Server  2000 - 8.00.2039 (Intel X86) 
  May  3 2005 23:18:38 
  Copyright (c) 1988-2003 Microsoft Corporation
  Developer Edition on Windows NT 6.0 (Build 6001: Service Pack 1)

(1 row(s) affected)
Server: Msg 139, Level 15, State 1, Line 1
Cannot assign a default value to a local variable.
Server: Msg 139, Level 15, State 1, Line 2
Cannot assign a default value to a local variable.
Server: Msg 139, Level 15, State 1, Line 3
Cannot assign a default value to a local variable.
Server: Msg 137, Level 15, State 1, Line 4
Must declare the variable '@salary'.
Server: Msg 139, Level 15, State 1, Line 5
Cannot assign a default value to a local variable.
Server: Msg 137, Level 15, State 1, Line 6
Must declare the variable '@firstname'.
Server: Msg 137, Level 15, State 1, Line 7
Must declare the variable '@Lastname'.
Server: Msg 137, Level 15, State 1, Line 8
Must declare the variable '@Salary'.
Server: Msg 137, Level 15, State 1, Line 9
Must declare the variable '@bonus'.
Server: Msg 137, Level 15, State 1, Line 10
Must declare the variable '@address'.


Fig 1.0

Results in 2005 Machine

----------------------------------------------------------
Microsoft SQL Server 2005 - 9.00.3042.00 (Intel X86) 
 Feb  9 2007 22:47:07 
 Copyright (c) 1988-2005 Microsoft Corporation
 Developer Edition on Windows NT 6.0 (Build 6001: Service Pack 1)

(1 row(s) affected)
Msg 139, Level 15, State 1, Line 0
Cannot assign a default value to a local variable.
Msg 139, Level 15, State 1, Line 0
Cannot assign a default value to a local variable.
Msg 139, Level 15, State 1, Line 0
Cannot assign a default value to a local variable.
Msg 137, Level 15, State 2, Line 4
Must declare the scalar variable "@salary".
Msg 139, Level 15, State 1, Line 0
Cannot assign a default value to a local variable.
Msg 137, Level 15, State 2, Line 6
Must declare the scalar variable "@firstname".
Msg 137, Level 15, State 2, Line 7
Must declare the scalar variable "@Lastname".
Msg 137, Level 15, State 2, Line 8
Must declare the scalar variable "@Salary".
Msg 137, Level 15, State 2, Line 9
Must declare the scalar variable "@bonus".
Msg 137, Level 15, State 2, Line 10
Must declare the scalar variable "@address".


Fig 1.1

In SQL Server 2008, we can declare and assign a variable at the same time as shown below.

--------------------------------------------------------------------------
Microsoft SQL Server code name "Katmai" (CTP) - 10.0.1300.13 (Intel X86) 
 Feb  8 2008 00:06:52 
 Copyright (c) 1988-2007 Microsoft Corporation
 Developer Edition on Windows NT 5.1 <X86> (Build 2600: Service Pack 2)

(1 row(s) affected)
Firstname
-------------------------------------------------------------------------
Derick
(1 row(s) affected)
Lastname
-------------------------------------------------------------------------
Scott
(1 row(s) affected)
Salary
---------------------
50000.00
(1 row(s) affected)
Bonus
---------------------
5000.00
(1 row(s) affected)
Address
---------------------------------------------------------------
90, Essex st, Hackensack, NJ
(1 row(s) affected)

We can also manipulate data at the time of assigning values to the variable as shown below.

select @@version
go
declare @firstname varchar(100) = 'Derick'
declare @lastname varchar(100) = 'Scott'
declare @Fullname varchar(100) = 'Mr.' + upper(@firstname)+upper(@Lastname)
declare @salary money = 50000
declare @bonus money = @salary*.10
declare @tax money = (@salary+@bonus) * .30 
declare @netsalary money =(@salary+@bonus) - @Tax
declare @address varchar(100) = '90, Essex st, Hackensack, NJ'
select @firstname as Firstname
select @Lastname as Lastname
select @Salary as BaseSalary
select @bonus as Bonus
select @tax as Tax
select @netsalary as NetSalary
select @address as Address
go

Results [Refer Fig 1.3]

-------------------------------------------------------------------------
Microsoft SQL Server code name "Katmai" (CTP) - 10.0.1300.13 (Intel X86) 
 Feb  8 2008 00:06:52 
 Copyright (c) 1988-2007 Microsoft Corporation
 Developer Edition on Windows NT 5.1 <X86> (Build 2600: Service Pack 2)

(1 row(s) affected)
Firstname
-------------------------------------------------------------------------
Derick
(1 row(s) affected)
Lastname
--------------------------------------------------------------------------
Scott
(1 row(s) affected)
BaseSalary
---------------------
50000.00
(1 row(s) affected)
Bonus
---------------------
5000.00
(1 row(s) affected)
Tax
---------------------
16500.00
(1 row(s) affected)
NetSalary
---------------------
38500.00
(1 row(s) affected)
Address
-----------------------------------------------------------------------
90, Essex st, Hackensack, NJ
(1 row(s) affected)

Conclusion

This article illustrated the TSQL enhancement made by Microsoft in SQL Server 2008 on variable declaration.

» See All Articles by Columnist MAK








The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers