title: Insert into 2 tables with cursor, then use returned scope_identity to insert into another table
body: <p>Basically I want to use an existing table, lets call it <code>T1</code>. I have to take that table, row by row and insert different columns into 2 separate tables. For example, <code>C1</code>, <code>C2</code> into <code>T2</code> and <code>C3</code>,<code>C4</code> into <code>T3</code>. </p>
  
  <p>During both of these inserts I need to make sure that the values that I am inserting do not already exist. Unfortunately there are multiple duplicates. Its not my data and it is very dirty. I have to do a ton of casting as is.
  Chances are good but not 100% that the column that I want to insert into <code>T2</code> or <code>T3</code> may exist while the other does not.</p>
  
  <p>Once those inserts are done I need a <code>@SCOPE_Identity</code> or another way to uniquely identify and hold in two declared values the auto incremented ID's that <code>T2</code> and <code>T3</code> create.</p>
  
  <p>These need to then be inserted into <code>T4</code> which is a lookup table that mostly only stores FK, its own ID, a comment and a BIT.</p>
  
  <p>I know it is a bit of a task, but I really need some help here.
  I have tinkered with multiple cursors and loops, but haven't got there yet.
  If I figure something out Ill post a solution, if nobody figures it out before me. </p>
  
  <p><strong>EDIT:</strong>
  So I worked it out. I have posted my code that has been made easy to read and use as an answer. If anyone wants to look at it, comment, make edits etc it will be there. There may be a better way to do it, so please comment if you can.</p>