2012-11-12 114 views
17

基本的には、タイトルの内容です。これは私のコードです。'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' 

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 VIEWを別々に実行すると、エラーなしで正しく実行されているようです。しかし、スクリプト全体を実行しようとすると、このエラーが表示されます。

メッセージ111、レベル15、状態1、行20
'VIEWを作成' クエリ・バッチの最初のステートメントでなければなりません。
メッセージレベル111、レベル15、状態1、行15
'CREATE VIEW'は、クエリバッチの最初のステートメントでなければなりません。
メッセージ208、レベル16、状態1、行2
オブジェクト名 'playerView'が無効です。

このスクリプトを実行する前に、まずデータベースを削除し、テーブルを再作成し、それらを移植してからこのスクリプトを実行します。

私は間違っていますか?

+1

すなわちただ文の前に '行く' 'ビューを作成...' を入れてください。 – sventevit

+0

@Shivarnあなたは決して答えを選択していません –

+0

@Omar Jackmanあなたの答えとDamien_The_Unbeliever理論は正しいです。あなたの答えは私にそれをはっきりと理解させ、私のコードを修正するのを助けたものでしたが。おかげでたくさん – Shivarn

答えて

25

PRINT 'Creating Player View'GOを入れて、それが動作するはずです:

PRINT 'Creating Player View' 
GO 

CREATE VIEW playerView AS 
+1

とても簡単です。ご不便おかけしてすみません。助けてくれてありがとう。それは今働く。 – Shivarn

+0

これでは不十分です。 ANSI_NULLSをONに設定し、 をSET QUOTED_IDENTIFIERに設定する必要があります。また、すべてをTRANSACTION – Fandango68

15

バッチは言葉GOで区切られます - 特にあなたのクエリを分割する方法をこれらのツールを伝える、クライアントツールに、ではないSQL Serverへの命令でありますバッチに

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 

は、だから私は、CREATE VIEWコードを入れGO

+0

内に配置することもできます。今、私は分かる :) – Shivarn

1

CREATE VIEW前に内部

SOME CONDITION.. 

EXECUTE('CREATE VIEW vwName...') 
EXECUTE追加しました:

エラーがCREATE VIEWがバッチの最初のステートメントでなければならないことを示しています

0

これは通常、VIEWまたは任意のDBOを作成できるため、スクリプト全体をTRANSACTIONまたは SET QUOTED_IDENTIFIERをオンにする必要があります。

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 
関連する問題