|
-
Jan 5th, 2009, 02:56 PM
#1
[RESOLVED] SQL Server 2005 Error On Create View
I'm having an problem with this SQL:
sql Code:
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'))
BEGIN
Create VIEW [Reporting].[Vw_RoomBlock_ReportingRoomTotals_1]
AS
Select
distinct(RoomBlock.RoomBlockId),
EntityType.Name as EntityType,
RoomBlock.EntityId,
RoomBlock.EntityTypeId,
RoomBlock.StartDate,
temp.RoomRevenue as RoomRevenue,
RoomBlock.TotalRooms as CalculatedRoomTotal,
RoomBlock.AlternateStartDate,
RoomBlock.IsAlternateStartDateSelected,
RoomBlock.TotalRooms,
RoomBlock.IsSenderCopy,
BusinessDataInfo.BusinessDataId,
BusinessDataInfo.BusinessDataTypeId,
BusinessDataType.Name as BusinessDataType,
RoomBlock.Budget as RoomBlockBudget
from
RoomBlock with (NOLOCK)
left join RoomRequirement with (NOLOCK)
on RoomBlock.RoomBlockId=RoomRequirement.RoomBlockId
left join RoomAvailability with (NOLOCK)
on RoomBlock.RoomBlockId=RoomAvailability.RoomBlockId
left Join BusinessDataInfo with (NOLOCK)
on RoomBlock.EntityId=BusinessDataInfo.BusinessDataInfoId
and RoomBlock.EntityTypeId=14
left join BusinessDataType with (NOLOCK)
on BusinessDataInfo.BusinessDataTypeId=BusinessDataType.BusinessDataTypeId
left join EntityType with (NOLOCK)
on EntityType.EntityTypeId=RoomBlock.EntityTypeId
left outer join
(SELECT RoomBlockId,sum(isnull(RoomAvailability.SingleQuantity,0)*isnull(RoomAvailability.SingleRate,0)
+ isnull(RoomAvailability.DoubleQuantity,0)*isnull(RoomAvailability.DoubleRate,0)
+ isnull(RoomAvailability.TripleQuantity,0)*isnull(RoomAvailability.TripleRate,0)
+ isnull(RoomAvailability.QuadQuantity,0) * isnull(RoomAvailability.QuadRate,0)) as 'RoomRevenue'
from RoomAvailability group by RoomBlockId) temp
on temp.roomblockid = RoomBlock.RoomBlockId
GO
GRANT SELECT ON Reporting.Vw_RoomBlock_ReportingRoomTotals_1 TO PUBLIC
GO
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
Sometimes the Programmer
Sometimes the DBA
Mazz1
-
Jan 5th, 2009, 03:18 PM
#2
Re: SQL Server 2005 Error On Create View
The Create View statement must be the first statement in a batch. The code should be
If Exists (Select ...
Drop the View
Go
Create View...
Go
-
Jan 5th, 2009, 03:20 PM
#3
Re: SQL Server 2005 Error On Create View
But I don't want to drop the view if there just create if it is not. Do I have to Drop then Create?
Sometimes the Programmer
Sometimes the DBA
Mazz1
-
Jan 5th, 2009, 03:26 PM
#4
Re: SQL Server 2005 Error On Create View
You could use Exec or sp_ExecuteSQL with a dynamic string to run a separate batch that creates the view.
-
Jan 5th, 2009, 04:50 PM
#5
Re: SQL Server 2005 Error On Create View
Actually, the preferred method is to drop it if it exsists, then re-create it. That allows you to have a script that you can use to update the view in the future (adding or removing a column for instance). The problem is your GOs can't be inside the Begin End block, as it causes the block to terminate. If you want to keep it in the same structure, remove the
Code:
GO
GRANT SELECT ON Reporting.Vw_RoomBlock_ReportingRoomTotals_1 TO PUBLIC
GO
to outside the Begin/end....
-tg
-
Jan 6th, 2009, 08:47 AM
#6
Re: SQL Server 2005 Error On Create View
Ok.... I went with tg suggestion (just much easier) Drop the objects owned by the scheam, then the schema. Cerate the schema and then the views.
Sometimes the Programmer
Sometimes the DBA
Mazz1
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|