title: Adding a bit column in stored procedure select
body: <p>I want to create a stored procedure that returns a row and an additional Bit column that says if that item is being used in other tables.</p>
  
  <p>Principal Table: PrincipalId, RowA, RowB</p>
  
  <p>TableA: TableAId, PrincipalId, NameA</p>
  
  <p>TableB: TableBId, PrincipalId, NameB</p>
  
  <p>TableC: TableCId, PrincipalId, NameC</p>
  
  <p>How can I compare the PrincipalId to know if it is being used and add that result in the final select?</p>
  
  <p>Thanks for your help.</p>
  
  <pre><code>ALTER PROCEDURE sp_available
  @principalId int
  AS
  BEGIN
  
      declare @available int
      set @available = 1
      IF EXISTS(select distinct PrincipalId from TableA where PrincipalId = @principalId
      and select distinct PrincipalId from TableB where PrincipalId = @principalId
      and select distinct PrincipalId from TableB where PrincipalId = @principalId)
          BEGIN
              set @available = 0
          END
  
      select RowA, RowB, @available as 'Available'
      from Principal
      where PrincipalId = @principalId
  END
  GO
  
  exec sp_available 2
  </code></pre>