0

Kindly eleborate me the difference Between Set and Select clause while Providing value to a Variable or Even a Column.

Declare @var nvarchar(Max)

Set @var= (Select TestName+ cAst(TestId as nvarchar) from TblTest where TestID=1)

Insert into TblTest(TestName)
Values (@var)

--------------------------------

Declare @var nvarchar(Max)

Select @var= (Select TestNAme + Cast(TestId as Nvarchar(MAx)) from TblTest Where TestID=1)

Insert into TblTest(TestName)
Values(@var)


-------------------------------

Values and result I am getting is Same in Both. But just wondering if there is any Difference. and if Any which Should I prefer. And Which one uses less Resources.

  • Try assigning to two variables in each of examples `@var` and `@var1` – billinkc Mar 31 '14 at 04:51
  • possible duplicate of [T-SQL - SET versus SELECT when assigning variables?](http://stackoverflow.com/questions/3945361/t-sql-set-versus-select-when-assigning-variables) – billinkc Mar 31 '14 at 04:52
  • Set variable is used for assigning the values to a variable in your case the result of your query is store in var. – user3217843 Mar 31 '14 at 04:52

1 Answers1

0

Here is an answer to my question, Initializing a variable with Select commands consumes lesser resources than using Set command. Other than this Usability is easy and compact.

I wont need to Write Set again and again.