I'm having an problem with this SQL:


sql Code:
  1. IF NOT EXISTS(SELECT [name] FROM SYS.VIEWS WHERE [name] = 'Vw_RoomBlock_ReportingRoomTotals_1' AND schema_id = (SELECT schema_id FROM SYS.SCHEMAS WHERE [name] = 'Reporting'))
  2. BEGIN
  3.     Create  VIEW [Reporting].[Vw_RoomBlock_ReportingRoomTotals_1]
  4.     AS
  5.  
  6.     Select
  7.         distinct(RoomBlock.RoomBlockId),
  8.         EntityType.Name as EntityType,
  9.         RoomBlock.EntityId,
  10.         RoomBlock.EntityTypeId,
  11.         RoomBlock.StartDate,
  12.         temp.RoomRevenue as RoomRevenue,
  13.         RoomBlock.TotalRooms as CalculatedRoomTotal,
  14.         RoomBlock.AlternateStartDate,
  15.         RoomBlock.IsAlternateStartDateSelected,
  16.         RoomBlock.TotalRooms,
  17.         RoomBlock.IsSenderCopy,
  18.         BusinessDataInfo.BusinessDataId,
  19.         BusinessDataInfo.BusinessDataTypeId,
  20.         BusinessDataType.Name as BusinessDataType,
  21.         RoomBlock.Budget as RoomBlockBudget
  22.     from  
  23.         RoomBlock with (NOLOCK)
  24.     left join RoomRequirement with (NOLOCK)
  25.     on RoomBlock.RoomBlockId=RoomRequirement.RoomBlockId
  26.     left join RoomAvailability with (NOLOCK)
  27.     on RoomBlock.RoomBlockId=RoomAvailability.RoomBlockId
  28.     left Join BusinessDataInfo with (NOLOCK)
  29.     on RoomBlock.EntityId=BusinessDataInfo.BusinessDataInfoId
  30.     and RoomBlock.EntityTypeId=14
  31.     left join BusinessDataType with (NOLOCK)
  32.     on BusinessDataInfo.BusinessDataTypeId=BusinessDataType.BusinessDataTypeId
  33.     left join EntityType with (NOLOCK)
  34.     on EntityType.EntityTypeId=RoomBlock.EntityTypeId
  35.     left outer join
  36.     (SELECT RoomBlockId,sum(isnull(RoomAvailability.SingleQuantity,0)*isnull(RoomAvailability.SingleRate,0)
  37.     + isnull(RoomAvailability.DoubleQuantity,0)*isnull(RoomAvailability.DoubleRate,0)
  38.     + isnull(RoomAvailability.TripleQuantity,0)*isnull(RoomAvailability.TripleRate,0)
  39.     + isnull(RoomAvailability.QuadQuantity,0) * isnull(RoomAvailability.QuadRate,0)) as 'RoomRevenue'
  40.     from RoomAvailability group by RoomBlockId) temp
  41.     on temp.roomblockid = RoomBlock.RoomBlockId
  42.  
  43.     GO
  44.     GRANT SELECT ON Reporting.Vw_RoomBlock_ReportingRoomTotals_1 TO PUBLIC
  45.     GO
  46. END

The error is:
Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'VIEW'.
Msg 102, Level 15, State 1, Line 53
Incorrect syntax near 'RoomBlockId'.
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near 'END'.

If I run the create view outside the If BLock it works.

Thanks
Gary