title: How to loop through a variable of type table in SQL Server?
body: <p>This is continuity of my first question in which I wanted to store multiple values in SQL Server variable.I need to loop through @tranId variable which holds two values and then delete a transaction based on the ids I have in @tranId. </p>
  
  <p>This is how my query looks like. </p>
  
  <pre><code>ALTER PROCEDURE "ESG"."SP_ADD_TEST_DATA"
  AS
  BEGIN
  
  DECLARE @envelopeId table(identifiers VARCHAR(1000));
  
  INSERT INTO @envelopeId (identifiers) VALUES('axaa1aaa-aaaa-a5aa-aaaa-aa8aaaa9aaaa');
  INSERT INTO @envelopeId (identifiers) VALUES('bxbb1bbb-bbbb-b5bb-bbb4-bb8bbbb9bbbf');
  
  DECLARE @tranId table(ids INT);
  INSERT INTO @tranId SELECT DOCUMENT_SET_TRANSACTION_ID FROM ESG.DOCUMENT_SET_TRANSACTION WHERE IDENTIFIER IN (SELECT identifiers FROM @envelopeId); 
  
  BEGIN
      DECLARE @transactionId int;
      DECLARE @tranId_cursor CURSOR;
      SET @tranId_cursor = CURSOR FOR 
      SELECT * FROM @tranId;
  
      OPEN @tranId_cursor
      FETCH NEXT FROM @tranId_cursor INTO @transactionId;
  
      WHILE @@FETCH_STATUS = 0
      BEGIN
              DELETE FROM ESG.DOCUMENT_SET_TRANS_MDATA WHERE DOCUMENT_SET_TRANSACTION_ID = @transactionId;
      END
  END
  CLOSE @tranId_cursor;
  DEALLOCATE @tranId_cursor;
  END
  </code></pre>
  
  <p>This is getting complex and I am not sure how to achieve this gracefully. </p>
  
  <p>Thanks</p>