title: Get multiple values in to a variable and looping through that values
body: <p>I am trying to put multiple values into a variable and looping through that values and I tried the below approach:</p>
  <pre><code>Table:Dept
      Deptid DeptName
      10      IT
      20      Admin
  Declaring a table variable:
      DECLARE @LOOP INT
      DECLARE @COUNTOFRECORDS INT
      DECLARE @x TABLE(
      r_no int not null primary key identity(1,1)
      dept_name VARCHAR(20))
  
      Insert into @x select deptname from dept
      
      SELECT @LOOP = 1
      SELECT @COUNTOFRECORDS = COUNT(r_no) from @x
      while(@LOOP &lt;= @COUNTOFRECORDS)
      BEGIN
      SELECT @x.dept_name from @x
      END
  </code></pre>