'CREATE VIEW' must be the first statement in a query batch

后端 未结 5 682
逝去的感伤
逝去的感伤 2020-12-09 15:36

Basically its what the title says. This is my code.

USE Assignment2;
GO

/* Player View (2 marks)
    Create a view which shows the following details of all          


        
相关标签:
5条回答
  • 2020-12-09 16:07

    This usually happens because being able to create a VIEW or any DBO, you require the entire script to be inside a TRANSACTION or you need to turn on SET QUOTED_IDENTIFIER.

    ie

    USE [yourdatabase]
    GO
    
    SET NUMERIC_ROUNDABORT, IMPLICIT_TRANSACTIONS OFF
    SET ANSI_PADDING, ANSI_NULLS, QUOTED_IDENTIFIER, ANSI_WARNINGS, CONCAT_NULL_YIELDS_NULL, ARITHABORT, XACT_ABORT ON
    SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
    GO
    
    BEGIN TRANSACTION
    GO
    
    SET ANSI_NULLS ON
    SET QUOTED_IDENTIFIER ON
    
    ...
    ...
    
    -- Write your Race View here
    PRINT 'Creating Race View'
    GO
    
    CREATE VIEW raceView AS 
    SELECT race.id, race.dateOfRace, race.raceType, raceType.name AS raceTypeName, race.course, course.name AS courseName, race.team, team.name AS teamName, race.observer, obs.firstName + ' ' + obs.surname AS observer_name, race.mvp, mvp.firstName + ' ' + mvp.surname AS mvp_name, race.pointsPenalised, race.finalScore + race.pointsPenalised AS unpenalised_score, race.finalScore
    FROM race
    INNER JOIN raceType
        ON race.raceType = raceType.id
        INNER JOIN course
            ON race.course = course.id
            INNER JOIN team
                ON race.team = team.id
                LEFT OUTER JOIN player AS mvp
                    ON race.mvp = mvp.id
                    LEFT OUTER JOIN player AS obs
                        ON race.observer = obs.id;
    GO 
    
    IF @@ERROR <> 0 BEGIN IF @@TRANCOUNT > 0 ROLLBACK SET NOEXEC ON END
    GO
    
    IF @@TRANCOUNT>0 COMMIT TRANSACTION
    GO
    
    SET NOEXEC OFF
    
    0 讨论(0)
  • 2020-12-09 16:12

    Put the CREATE VIEW Code inside EXECUTE

    SOME CONDITION..
    
    EXECUTE('CREATE  VIEW vwName...')
    
    0 讨论(0)
  • 2020-12-09 16:24

    put GO after PRINT 'Creating Player View' and it should work:

    PRINT 'Creating Player View'
    GO
    
    CREATE VIEW playerView AS
    
    0 讨论(0)
  • 2020-12-09 16:30

    Batches are delimited by the word GO - which is an instruction to client tools, not to SQL Server, specifically telling those tools how to split your query into batches.

    The error tells you that CREATE VIEW must be the first statement in a batch:

    USE Assignment2;
    GO
    
    /* Player View (2 marks)
        Create a view which shows the following details of all players:
            • The ID number of the player
            • The first name and surname of the player concatenated and given an alias of “full_name”
            • The team ID number of the player (if applicable)
            • The team name of the player (if applicable)
            • The coach ID number of the player (if applicable)
            • The name of the player’s coach (if applicable)
    
       Creating this view requires a select statement using multiple joins and concatenation of names.  
       Make sure that you use the appropriate type of join to ensure that players without teams or coaches are still included in the results.
    
    */
    
    
    -- Write your Player View here
    PRINT 'Creating Player View'
    
    GO -->-- New GO here
    
    CREATE VIEW playerView AS 
    

    So I've added a GO before CREATE VIEW

    0 讨论(0)
  • 2020-12-09 16:30

    You may also run into this issue if trying to execute scripts from Entity Framework migrations. I think this is due to EF running those scripts in a transaction (as mentioned in another answer to this question). You can get round that with this type of syntax:

    IF NOT EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[V_MovieActors]'))
    EXEC dbo.sp_executesql @statement = N'CREATE VIEW [dbo].[V_MovieActors]
    AS
    SELECT       NEWID() AS Id, dbo.Movie.Title, dbo.Movie.ReleaseDate, dbo.Actor.FirstName + '' '' + dbo.Actor.LastName AS Actor, dbo.Actor.DateOfBirth
    FROM            dbo.Actor INNER JOIN
                             dbo.Movie ON dbo.Actor.Id = dbo.Movie.Actor_Id
    '
    

    Which turns the whole thing into a single command for SQL to execute. That approach comes from this very useful article Using SQL Views With Entity Framework Code First by Morgan Kamoga.

    0 讨论(0)
提交回复
热议问题