Results 1 to 6 of 6

Thread: [RESOLVED] SQL Server 2005 Error On Create View

  1. #1

    Thread Starter
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Resolved [RESOLVED] SQL Server 2005 Error On Create View

    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
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  2. #2
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    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

  3. #3

    Thread Starter
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    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

  4. #4
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    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.

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6

    Thread Starter
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    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
  •  



Click Here to Expand Forum to Full Width