Good back-to-basics question, thanks
kapil190588 (10/10/2012)
Hi,I am new in field of DBA...
I just want to know that can we assign value to a variable through SELECT statement also as done in the
question
Select @STR=1,
whts the difference between -
Set @STR=1
select @STR=1
in this quesion?
The final effect is the same, i.e. the variable @STR is assigned the value of 1.
There are numerous minor differences between the actions.
the main difference is that the SET can be used to allocate a value to one variable at a time, while a SELECT can allocate multiple at the same time, e.g.
DECLARE @Var1 INT, @Var2 VARCHAR(25)
SET @Var1 = 5;
SET @Var2 = 'Hello ';
--OR--
SELECT @Var1 = 5, @Var2 = 'Hello '
With the advent of SQL2008, it is now possible to assign a value to a variable when declaring it (just as is done in coding in e.g. C# or vb, e.g.
DECLARE @Var1 INT = 5, @Var2 VARCHAR(25) = 'Hello '
SELECT @Var1 *= @Var1, -- will render as 25
@Var2 += SUSER_SNAME()
SELECT @Var1, @Var2
____________________________________________
Space, the final frontier? not any more...
All limits henceforth are self-imposed.
“libera tute vulgaris ex”