title: A constraint with function that contains Select is never satisfied
body: <p>I have a table like this:</p>
  
  <pre><code>CREATE TABLE CarRentSchedule
  (
      carId INTEGER NOT NULL,
      rent_start INTEGER NOT NULL,
      rent_end INTEGER NOT NULL,
      personId INTEGER NOT NULL,
      tariff INTEGER NOT NULL,
      PRIMARY KEY carId,
      FOREIGN KEY (personId) REFERENCES Persons(PersonID)
      CONSTRAINT CHK_CR_AVLBL CHECK (dbo.CarCheck(carId , rent_start , rent_end ) = 1))
  </code></pre>
  
  <p>And I need to forbid any rent time intersection for the same car.
  I found that, when a function is used (example below) inside the constraint, the value it returns is different from whent it is used outside the constraint.</p>
  
  <pre><code>if exists (select * from dbo.CarRentSchedule rt
                 where rt.carId = @id 
                   and ((rt.rent_Start &gt;= @startTime and rt.rent_end &lt; @endTime)
                        or  
                        (rt.rent_end &lt;= @endTime and rt.rent_end &gt; @startTime)
                        or 
                        (rt.rent_Start &lt; @endTime and rt.rent_end &gt; @startTime)
                        or 
                        (rt.rent_Start &gt;= @startTime and rt.rent_end &lt;= @endTime)))
          return 0
  </code></pre>
  
  <p>So when I'm trying to insert data into the table, the constraint would never allow me to for some reason. </p>
  
  <p>I have checked, when called from the constraint the function seems to return NULL, however calling the function like </p>
  
  <pre><code>select dbo.CheckFunc(1, 10, 15, 1, 1) 
  </code></pre>
  
  <p>would return 0/1 as it is supposed to. I have also checked and found out that if there is no SELECT in the function (a simple comparison operation, for example) it would work just fine in the constraint too.</p>
  
  <p>Could anybody explain why does this work this way? Is it some restriction forbidding using SELECT's inside the constraints or is it because the SELECT references to the same table the constraint applied to?</p>