Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql “创建视图”必须是查询批处理中的第一条语句_Sql_Sql Server 2008 - Fatal编程技术网

Sql “创建视图”必须是查询批处理中的第一条语句

Sql “创建视图”必须是查询批处理中的第一条语句,sql,sql-server-2008,Sql,Sql Server 2008,基本上这就是标题所说的。这是我的密码 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 al

基本上这就是标题所说的。这是我的密码

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.
*/
当我分别运行每个CREATE视图时,它似乎可以正确运行,没有错误。但是当我尝试运行整个脚本时,它会给我这个错误

味精111,15级,状态1,第20行 “创建视图”必须是查询批处理中的第一条语句。 味精111,15级,状态1,第15行 “创建视图”必须是查询批处理中的第一条语句。 Msg 208,16级,状态1,第2行 无效的对象名称“playerView”

在尝试运行此脚本之前,我首先删除数据库,重新创建表,填充它们,然后运行此脚本

你知道我哪里出了问题吗?

在打印“创建玩家视图”之后加上GO,应该可以:

PRINT 'Creating Player View'
GO

CREATE VIEW playerView AS

批由单词GO分隔,GO是对客户端工具的指令,而不是对SQL Server的指令,专门告诉这些工具如何将查询拆分为批

该错误告诉您CREATE VIEW必须是批处理中的第一条语句:

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 

因此,我在创建视图之前添加了一个GO-before-CREATE-VIEW

将创建视图代码放入EXECUTE中

SOME CONDITION..

EXECUTE('CREATE  VIEW vwName...')

这通常是因为要创建视图或任何DBO,需要将整个脚本放在事务中,或者需要打开 设置您的标识符


如果尝试执行实体框架迁移中的脚本,也可能会遇到此问题。我认为这是由于EF在一个事务中运行这些脚本,正如对这个问题的另一个回答中提到的。您可以使用以下类型的语法:

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
'

这将整个过程转换为一个命令,供SQL执行。这种方法来自这篇非常有用的文章。

在“创建视图…”之前加上一个“go”。@Shivarn您从未选择过answer@Omar杰克曼,你的答案和达米恩,不信者理论是正确的。虽然你的回答让我明白了,并且帮助我修复了代码。非常感谢这么简单的lol。很抱歉给您带来不便。谢谢你的帮助。现在可以了,这还不够。您需要将ANSI_NULL设置为ON,并将QUOTED_IDENTIFIER设置为ON,还可以将其全部放在事务中谢谢:。我现在明白了:当您想在多个块中使用一个变量(包括CREATETABLE语句)时,这也很有用,因为变量是声明它们的批的局部变量。
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
'