Q: How to use cursor to create loop in a temporary table in SQL Server I'm new to coding.
Basically, I'm trying to see which cars were used in 12 days. To do that, I've created a table named CAR_REQUEST, which saves any requests about using a car (a 4-seater car, for example). After a person, whose Id is included in "UserId" column of "CAR_REQUEST" table,  sends a car request to me, I will decide if I agree to let him use it or not. If yes, "isApproved" (which is in "CAR_REQUEST" table) will be equal to 1. Otherwise, it will be equal to 0.
After that request is included, in this case, I've created a temporary table named #GETCarList which will take the datas from UserId, isApproved from CAR_REQUEST and CarName, License from CAR_LIST (which includes the list of the cars I will supply for the requested people). After that, I've created some columns from [T+0] to [T+11]. These columns will check if "isApproved" is equal to 1 for which cars in those days or not. If yes, these columns will be in "BLUE" color. If not, they will be in "WHITE" color. For example, if [T+0] = 1, and the car I will supply from CAR_LIST is Toyota Camry 2005, it will be in "BLUE" color. However, since if I do this in all 12 columns from [T+0] to [T+11], the code will be too long, I have to declare @count and set it to 0, then I create a WHILE loop to check if "isApproved" in [T+@count] is equal to 1 or not. If yes, [T+@count] will be in BLUE color. If not, it will be in WHITE color. However, SQL says that my syntax is wrong and I don't know how to solve this.
Here is the code:
DECLARE @ID INT
 DECLARE @count INT
  set @count = 0
  DECLARE @Color nvarchar(50)
  create TABLE #GETCarList 
  (
      Id INT identity primary key not null,
      UserId int,
      CARNAME nvarchar(50),
      BKS nvarchar(50),
      [T+0] int,
      [T+1] int,
      [T+2] int,
      [T+3] int,
      [T+4] int,
      [T+5] int,
      [T+6] int,
      [T+7] int,
      [T+8] int,
      [T+9] int,
      [T+10] int,
      [T+11] int
  )
  Insert into #GETCarList(CARNAME, License) select u.CarName, u.License from CAR_LIST u
  DECLARE GetCar CURSOR FOR SELECT * FROM #GETCarList
  OPEN GetCar 
  FETCH NEXT FROM GetCar INTO @Color,@count
  WHILE (@@FETCH_STATUS = 0)
  begin
       BEGIN
            select distinct #GETCarList.Id, #GETCarList.UserId, #GETCarList.NAME, #GETCarList.License, d.Trangthai
            from #GETCarList
            inner join CAR_REQUEST d on #GETCarList.UserId = d.Id
            WHILE @cnt < 11
            begin
                set [T+@count] = d.isApproved where #GETCarList.Id = @ID
                case #GETCarList.[T+@count]
                when 1 then SET @Color = 'BLUE'
                when 0 then SET @Color = 'WHITE'
                SET @count = @count + 1
                FETCH NEXT FROM GetCar INTO @Color,@count
            end
       END 
   END

