name="msvalidate.01" content="439BE949614BBF57E29AEDBC5087737D" /> learning sql server: Variable Declaration and Assignment

Rabu, 24 Juni 2009

Variable Declaration and Assignment

A long time ago, I learned that T-SQL was not like other programming languages. For a long
time, I accepted that when I declared a variable, all I could do was to declare it. Sure, stored
procedure and user-defi ned function parameters could be declared and set to a default, but
that simply didn’t happen for inline variable declarations in T-SQL. That is, until now.
Indeed, it is true. You can now declare and assign a variable in one statement. So instead of
writing this code:
DECLARE
@someIntVal INT,
@someStringVal varchar(100)
SET @someIntVal = 1
SET @someStringVal = ‘One’
You can now write this code:
DECLARE
@someIntVal INT = 1,
@someStringVal varchar(100) = ‘One’
I know it seems like such a simple change, but believe me, it adds up over time. For example,
look at this set of variable declarations and assignments from the aspnet_Membership_
CreateUser stored procedure:
DECLARE
@someIntVal INT,
@someStringVal varchar(100)
SET @someIntVal = 1
SET @someStringVal = ‘One’
DECLARE
@someIntVal INT = 1,
@someStringVal varchar(100) = ‘

1 komentar: