Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 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'  CREATE VIEW playerView AS  SELECT player.id, player.firstName + ' ' + player.surname AS 'Full name', player.team, team.name, player.coach, coach.firstName, coach.surname  FROM player LEFT OUTER JOIN team     ON player.team = team.id     LEFT OUTER JOIN player as coach         ON player.coach = coach.id;    GO /* Race View (3 marks)    Create a view which shows the following details of all races:         • All of the columns in the race table         • The name of the race type, course and team involved in the race         • The full name of the player observing the race and the full name of the MVP (if applicable)         • A calculated column with an alias of “unpenalised_score”, which adds the points penalised to the final score     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 races without MVPs are still included in the results. */  -- Write your Race View here PRINT 'Creating Race View'  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   SELECT *  FROM playerView  SELECT * FROM raceView   /* Additional Information:    The views are very convenient replacements for the tables they represent, as they include the names and calculated values that you will often need in queries.    You are very much encouraged to use the views to simplify the queries that follow.  You can use a view in a SELECT statement in exactly the same way as you can use a table.     If you wish to create additional views to simplify the queries which follow, include them in this file. */ 

When I run each CREATE VIEW separately, it seems to run it correctly with no errors. But when I try to run the entire script, it gives me this error.

Msg 111, Level 15, State 1, Line 20
'CREATE VIEW' must be the first statement in a query batch.
Msg 111, Level 15, State 1, Line 15
'CREATE VIEW' must be the first statement in a query batch.
Msg 208, Level 16, State 1, Line 2
Invalid object name 'playerView'.

Before attempting to run this script, I first delete database, recreate the tables, populate them and then run this script.

Any ideas where I'm going wrong?

like image 263
Shivarn Avatar asked Nov 12 '12 08:11

Shivarn


People also ask

When should you create a view from a select statement?

Views can be used for a few reasons. Some of the main reasons are as follows: To simplify database structure to the individuals using it. As a security mechanism to DBAs for allowing users to access data without granting them permissions to directly access the underlying base tables.

How do you create a query view?

To create a view, a user must have the appropriate system privilege according to the specific implementation. CREATE VIEW view_name AS SELECT column1, column2..... FROM table_name WHERE [condition]; You can include multiple tables in your SELECT statement in a similar way as you use them in a normal SQL SELECT query.

What is CREATE VIEW command in SQL?

The CREATE VIEW command creates a view. A view is a virtual table based on the result set of an SQL statement.

How do I create a view in SQL Server?

SQL Server CREATE VIEW First, specify the name of the view after the CREATE VIEW keywords. The schema_name is the name of the schema to which the view belongs. Second, specify a SELECT statement ( select_statement ) that defines the view after the AS keyword. The SELECT statement can refer to one or more tables.


2 Answers

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

PRINT 'Creating Player View' GO  CREATE VIEW playerView AS 
like image 159
DiverseAndRemote.com Avatar answered Oct 11 '22 10:10

DiverseAndRemote.com


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

like image 32
Damien_The_Unbeliever Avatar answered Oct 11 '22 11:10

Damien_The_Unbeliever