2014-02-11 40 views
1

我有一組表如下:SQL服務器創建一個視圖和連接表

書,列:的bookcode,標題,PublisherCode,類型,平裝(Y/N)

作者,列: AuthorNum,AuthorLast,AuthorFirst(地名)

出版商,列:PublisherCode,PublisherName的,市(我不相信我現在需要這個右)

寫道:列:的bookcode,AuthorNum,序列

我需要創建一個名爲「FictionBestSellers」視圖,顯示書名,作者名,作者姓,作者和圖書類型

這是我有:

CREATE VIEW FictionBestSellers as Book, AuthorFname, AutheorLname, Paperback, btype 
select book.Title, Author.AuthorFirst, Author.AuthorLast, book.Paperback, book.type 
from Book, Author, Wrote 
book join Wrote 
on book.BookCode = wrote.BookCode 
where book.type = 'MYS' or book.type = 'SFI' or book.type = 'HOR' 

現在它是告訴我 「Msg 102,Level 15,State 1,Procedure FictionBestSellers,Line 1 'Book'附近語法錯誤。」

我在做什麼錯?

回答

2
CREATE VIEW FictionBestSellers(Book, AuthorFname, AutheorLname, Paperback, btype) 
AS 
select book.Title, Author.AuthorFirst, Author.AuthorLast, book.Paperback, book.type 
from Book 
    join wrote 
    on book.BookCode = wrote.BookCode 
    join Author 
    on wrote.AuthorNum= AuthorNum 
where book.type = 'MYS' or book.type = 'SFI' or book.type = 'HOR' 
+0

+1這句法VIEW_NAME(COLUMN_NAMES ...)不知道這是可能的:) –

+0

@ M.Ali這是可選的,但它是可能的。 http://technet.microsoft.com/en-us/library/ms187956.aspx –

+0

謝謝,但你提供的鏈接是在俄羅斯我認爲,我不會說俄羅斯:) –

2

這是正確的SQL。

CREATE VIEW FictionBestSellers 
AS 
SELECT b.Title AS Book 
, a.AuthorFirst AS AuthorFname 
, a.AuthorLast AS AutheorLname 
, b.Paperback AS Paperback 
, b.type AS BType 
FROM Wrote w   
    INNER JOIN Book b ON w.Bookcode = b.Bookcode 
    INNER JOIN Author a on w.AuthorNum = a.AuthorNum 
WHERE b.type IN ('MYS', 'SFI', 'HOR') 
1
CREATE VIEW FictionBestSellers 
as 
select book.Title   AS Book 
    , Author.AuthorFirst AS AuthorFname 
    , Author.AuthorLast AS AutheorLname 
    , book.Paperback  AS Paperback 
    , book.type   AS btype 
from Book INNER JOIN Wrote 
ON Book.Bookcode = Wrote.Bookcode 
INNER JOIN Author 
ON Wrote.AuthorNum = Author.AuthorNum 
where book.type = 'MYS' 
OR book.type = 'SFI' 
OR book.type = 'HOR'