title: How do I insert into a table if a value does not exist, but only for certain criteria? (MS SQL Server)
body: <p>Im very new to SQL but need to write a query to do the following.
  Using MS SQL Server 2005.</p>
  
  <pre>
  
  Profile                  DefinitioninProfile         Definition
  ------                   -------------------         ----------      
  ProfileID                DefinitionID                DefinitionID
  ProfileType              ProfileID                   DefinitionType
  ProfileName                                          
  
  </pre>
  
  <p>In the definition table, defintion Type can be TypeA, TypeB ..... TypeZ.
  I want to make sure that for a certain profile type, ProfileTypeA
  the Definition has all the Types, TypeA -> TypeZ.</p>
  
  <p>But some of the types already exist in the table, and I dont want to have duplicates. </p>
  
  <pre>
  So its something like
  SELECT ProfileID from Profile where ProfileType = ProfileTypeA
  FOR EACH ProfileID
     IF NOT EXISTS IN Defintion TypeA
     INSERT TypeA into Definition
     INSERT ProfileID, DefinitionID into DefinitionInProfile
  
     ...repeat for TypeB, TypeC...
  END
  </pre>
  
  <ol>
  <li><p>I need to get all the instances where ProfileType = ProfileTypeA</p></li>
  <li><p>Then get the first Profile.profileID</p></li>
  <li><p>Then check the DefinitioninProfile table to get a list of DefinitionIDs where the
  profileID = Profile.ProfileID</p></li>
  <li><p>Then for all those Definition IDs check if there is a definitionType called 'TypeA'
  if not insert it, if there is ignore it.
  Then do the same for 'TypeB', repeat for typec, .. typeZ</p></li>
  </ol>
  
  <p>Go back to step 2 and get the next Profile.ProfileID and repeat 3 &amp; 4 for that profile ID.</p>