DCSIMG
SQL Server 2008 T-SQL: DECLARE and SET in the Same Statement - Guy Burstein's Blog

Guy Burstein's Blog

Developer Evangelist @ Microsoft

News

Guy Burstein The Bu

Disclaimer
Postings are provided 'As Is' with no warranties and confer no rights.

Guy Burstein LinkedIn Profile

TwitterCounter for @bursteg

Links

Articles

Blogs I Read

SQL Server 2008 T-SQL: DECLARE and SET in the Same Statement

SQL Server 2008 T-SQL: DECLARE and SET in the Same Statement

SQL Server 2008 T-SQL DECLARE  SET

In SQL Server 2005, if you wanted to declare a new variable and set its value, you had to write both DELCARE and SET statements:

DECLARE @City VARCHAR(20)

SET @City = 'London'

A new T-SQL improvement in SQL Server 2008 is allows you to write DECLARE and SET in the Same Statement:

DECLARE @City VARCHAR(20) = 'London'

If you try this in SQL Server 2005, you'll get the following error message:

Msg 139, Level 15, State 1, Line 0
Cannot assign a default value to a local variable.

Not a big deal, but can get our code to look better!

Enjoy!

Comments

graffic said:

And we had to wait till 2008...

# October 22, 2008 6:17 AM

jj said:

cool

# April 2, 2009 2:19 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: