title: Insert values into table based on condition, sql server
body: <p>I try to insert values into my table in stored procedure based on the conditions:</p>
  
  <pre><code>alter procedure service_report_st2
  as
  insert into Service_report_step1(appointment_or_house) 
  select
  case
  when Service_report_step1.card_uid in(select card_uid from visits where vidpos_kod = 'A')
  then '1'
  
  when Service_report_step1.card_uid in(select card_uid from visits  where vidpos_kod = 'B')
  then '2'
  
  else '3'
  end
  </code></pre>
  
  <p>The error is in <code>Service_report_step1.card_uid</code> it is not found, however this is the first column in <code>Service_report_step1</code> table. <code>appointment_or_house</code> is the second column, which should contain '1', '2' or '3' if <code>visits</code> table contains <code>card_uid</code> in <code>Service_report_step1</code>.</p>
  
  <p>I'd appreciate any help!</p>
  
  <p>Short example:</p>
  
  <p><code>Visits</code> table has 2 columns: Card_uid and vidpos_kod</p>
  
  <pre><code>card_uid         vidpos_kod
   111-111              A
   222-222              B
   333-333              A
   444-444              C
  </code></pre>
  
  <p>now <code>Service_report_step1</code> table has card_uid (column that contains all possible cards) and appointment_or_house column</p>
  
  <pre><code>card_uid      appointment_or_house
   111-111              1
   222-222              2
   333-333              1
   444-444              1
  </code></pre>
  
  <p>So, If I have the same <code>card_uids</code> in <code>Service_report_step1</code> as in <code>Visits</code> I determine <code>appointment_or_house</code> column based on <code>vidpos_kod</code> in <code>Visits</code> (<code>A, C</code> is '1', <code>B</code> is '2')</p>
  
  <p>I need to insert into <code>Service_report_step1</code> all data correctly as in example.</p>