The multi-part identifier "xxx" could not be bound The following is a HAVING clause from a small part of a very large stored procedure which I have inherited from some other devs (external);

HAVING (SELECT COUNT(*) FROM
(
    SELECT *
    FROM dbo.ContractDailyRoomAllocation da2
    WHERE da2.ContractId = DA.ContractId
    AND da2.RoomTypeId = DA.RoomTypeId
    AND da2.Date &gt;= @FromDate AND da2.Date &lt; @ToDate
    AND da2.IsSold = 0
    AND da2.ReleaseDay &lt;= DATEDIFF("d", @TodayDate, da2.Date)) T) = @StayDates
    AND ( (@AllowOnRequestBookings = 'False' 
      OR dbo.GetAvailablePackageRoomCount(da2.ContractId,da2.RoomTypeId,@FromDate,@ToDate)  
      &gt;= 0 ) )
      AND ( (@AllowOnRequestBookings = 'True' 
      OR dbo.GetAvailablePackageRoomCount(da2.ContractId,da2.RoomTypeId,@FromDate,@ToDate) 
      &gt; 0 )
)


The last 2 AND's give me the following error messages:


  Msg 4104, Level 16, State 1, Procedure GetAvailableHotelRooms, Line 176
  The multi-part identifier "da2.ContractId" could not be bound.
  Msg 4104, Level 16, State 1, Procedure GetAvailableHotelRooms, Line 176
  The multi-part identifier "da2.RoomTypeId" could not be bound.
  Msg 4104, Level 16, State 1, Procedure GetAvailableHotelRooms, Line 177
  The multi-part identifier "da2.ContractId" could not be bound.
  Msg 4104, Level 16, State 1, Procedure GetAvailableHotelRooms, Line 177
  The multi-part identifier "da2.RoomTypeId" could not be bound.


Any ideas?
Reformatting your code shows the problem. All I have changed is whitespace and added a comment:

HAVING
    (
        SELECT COUNT(*) FROM
        (
            SELECT *
            FROM dbo.ContractDailyRoomAllocation da2 -- &lt;-- da2 defined here
            WHERE da2.ContractId = DA.ContractId
            AND da2.RoomTypeId = DA.RoomTypeId
            AND da2.Date &gt;= @FromDate AND da2.Date &lt; @ToDate
            AND da2.IsSold = 0
            AND da2.ReleaseDay &lt;= DATEDIFF("d", @TodayDate, da2.Date)
        ) T
    ) = @StayDates
AND
    ( 
        (
            @AllowOnRequestBookings = 'False' OR
            dbo.GetAvailablePackageRoomCount(da2.ContractId,da2.RoomTypeId,@FromDate,@ToDate) &gt;= 0

        )
    )
AND 
    (
        (
            @AllowOnRequestBookings = 'True' OR
            dbo.GetAvailablePackageRoomCount(da2.ContractId,da2.RoomTypeId,@FromDate,@ToDate) &gt; 0
        )
    )


As can now easily be seen, da2 is an alias defined in the subquery in the first ANDd term, and so is not available in other terms.

It may be that you want to move the second and third ANDd terms here into the subquery, or it might be more complicated than that.