2012-11-12 6 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
'दृश्य बनाते हैं' एक प्रश्न बैच में पहले बयान होना चाहिए।
संदेश 111, स्तर 15, राज्य 1, रेखा 15
'दृश्य बनाएं' एक क्वेरी बैच में पहला बयान होना चाहिए।
संदेश 208, स्तर 16, राज्य 1, रेखा 2
अवैध ऑब्जेक्ट नाम 'प्लेयर व्यू'।

इस स्क्रिप्ट को चलाने के लिए प्रयास करने से पहले, मैं पहली बार, डेटाबेस को हटाने टेबल पुन: बनाने, उन्हें पॉप्युलेट और फिर इस स्क्रिप्ट को चलाने।

कोई विचार जहां मैं गलत हो रहा हूं?

+1

रखो एक बस बयान से पहले 'जाना' 'दृश्य बनाते हैं ...'। – sventevit

+0

@ शिवरन आपने कभी जवाब नहीं दिया –

+0

@ ओमर जैकमैन आपका उत्तर और Damien_The_Unbeliever सिद्धांत सही था। यद्यपि आपका उत्तर वह था जिसने मुझे इसे स्पष्ट रूप से समझ लिया और मेरे कोड को ठीक करने में मदद की। बहुत बहुत धन्यवाद – Shivarn

उत्तर

25

PRINT 'Creating Player View' के बाद GO रख दिया और यह काम करना चाहिए:

PRINT 'Creating Player View' 
GO 

CREATE VIEW playerView AS 
+1

इतना आसान लॉल। असुविधा के लिए खेद है। सहायता के लिए धन्यवाद। यह अब काम करता है। – Shivarn

+0

यह पर्याप्त नहीं है। आपको ANSI_NULLS चालू और सेट QUOTED_IDENTIFIER सेट करने की आवश्यकता है, और आप इसे सभी को एक ट्रान्सएक्शन – Fandango68

15

बैचों शब्द GO द्वारा सीमांकित कर रहे हैं - जो ग्राहक उपकरणों के लिए एक निर्देश है, नहीं एसक्यूएल सर्वर, विशेष रूप से उन लोगों के उपकरण कैसे आपकी क्वेरी विभाजित करने के लिए कह रही करने के लिए बैचों में

त्रुटि आपको बताता है कि 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 

तो मैं एक बनाएं कोड देखें रखो GOCREATE VIEW से पहले

+0

के अंदर भी रख सकते हैं :) धन्यवाद। मैं अब समझता हूँ :) – Shivarn

1

अंदर निष्पादित

SOME CONDITION.. 

EXECUTE('CREATE VIEW vwName...') 
जोड़ दिया है
0

ऐसा आमतौर पर होता है क्योंकि दृश्य या किसी भी डीबीओ बनाने में सक्षम होने के कारण, आपको संपूर्ण स्क्रिप्ट को ट्रान्सएक्शन के अंदर होना आवश्यक है या आपको सेट 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