Good back-to-basics question, thanks
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
whts the difference between -
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.
Read about SET
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 ';
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”