title: SQL Server - Cursor
body: <p>I am trying to loop through a table using a cursor:</p>
  
  <pre><code>DEClARE @ProjectOID as nvarchar (100)
  DECLARE @TaskOID as nvarchar (100)
  DECLARE TaskOID_Cursor FOR
  SELECT  TaskOID FROM ProjectOID_Temp
  OPEN TaskOID_Cursor
  FETCH NEXT FROM TaskOID_Cursor INTO @TaskOID
  WHILE @@FETCH_STATUS = 0
  BEGIN
      SELECT t1.OID as taskResourceOID, t2.OID as EvUserOID
      FROM (select OID, resourceOID from taskresourcehours 
      where projecttaskoid =   @TaskOID) as t1,
      (
       select OID, workerOID
       from Evuser
       where workerOID in
        ( select resourceOID from taskresourcehours where projecttaskoid = @TaskOID )
      ) as t2
      WHERE t1.resourceOID = t2.workerOID
  FETCH NEXT FROM TaskOID_Cursor
  INTO @TaskOID
  END
  CLOSE TaskOID_Cursor
  DEALLOCATE TaskOID_Cursor
  </code></pre>
  
  <p>That above returns taskResourceOID and EvUserOID. If I need to output a table with the @TaskOID and the respective taskResourceOID and EvUserOID, what is the best way to do it? </p>