Just a day ago, while I was enjoying mini vacation during festival of Diwali I met one of the .NET developer who is big fan of Oracle. While discussing he suggested that he wished SQL Server should have feature where multiple variable can be declared in one statement. I requested him to not judge wonderful product like SQL Server with just one feature.
SQL Server is great product and it has many feature which are very unique to SQL Server. Regarding feature of SQL Server where multiple variable can be declared in one statement, it is absolutely possible to do.
Method 1: Multiple statements for declaring multiple variables
DECLARE @Var1 INT
DECLARE @Var2 INT
SET @Var1 = 1
SET @Var2 = 2
SELECT @Var1 'Var1', @Var2 'Var2'
GO
Method 2: Single statements for declaring multiple variables
DECLARE @Var1 INT, @Var2 INT
SET @Var1 = 1
SET @Var2 = 2
SELECT @Var1 'Var1', @Var2 'Var2'
GO
From above example it is clear that multiple variables can be declared in one statement. In SQL Server 2008 when variables are declared they can be assigned values as well. Please refer my previous article for the same SQL SERVER – 2008 – Two Convenient Features Inline Assignment – Inline Operations.
Reference : Pinal Dave (http://www.SQLAuthority.com)




Method 3: Single statements for declaring and defining multiple variables:
DECLARE @Var1 INT, @Var2 INT
SELECT @Var1 = 1, @Var2 = 2
SELECT @Var1 ‘Var1′, @Var2 ‘Var2′
GO
Same goes for assigning variables from a query
Select @Var1 = Col1, @Var2 = Col2 From Table …
Thanks for the tutorial.
I have a table like this:
TagKey TagID TagvALUE TagTimeStamp
I want to create this constraint:
“If the previous TagValue = current TagValue then do not update”.
Can you help me, please?. Thanks.
Can we undeclare variables? We can deallocate CURSOR varilable, how can we deallocate or undeclare local variable?
Thanks
J
I want display like under from
select Name=lastname+’ ‘+firstname from emp
but all firstname should be left align like under one column.
Thanks
Rajesh