2014-10-04 129 views
1

我正在嘗試編寫一個查詢來選擇此視圖。無法使用查詢創建表格

CREATE VIEW 
    CarRentalHistoryView 
AS 
    SELECT 
     c.vehNo, 
     c.brand, 
     c.dateRegistered, 
     c.purchasePrice, 
     c.rentalCost, 
     c.condition as 'Before Condition', 
     r.condition as 'After Condtion', 
     r.dateRented, 
     r.dateReturned, 
     r.CustNo,cu.name, 
     cu.id, 
     cu.dateOfBirth 
    FROM 
     dbo.Car c 
    Inner JOIN 
     dbo.Rental AS r 
    ON 
     c.VehNo = r.VehNo 
    Inner JOIN 
     dbo.Customer as cu 
    on 
     cu.CustNo = r.CustNo; 
GO 

這是我試圖運行查詢,

Select 
    * 
From 
    dbo.CarRentalHistoryView 
Where 
    'Before Condition' != 'After Condition' 

但查詢運行良好,即使是這種情況

Select 
    * 
From 
    dbo.CarRentalHistoryView 
Where 
    'Before Cion' != 'After Ction' 

所以我的觀點是怎麼樣,我可以確保我可以得到與以前不同的車輛結果。 TKS!

+0

切勿使用列別名單引號。 SQL Server中正確的轉義字符是方括號。我正在投票結束,因爲這是一個印刷錯誤。 – 2014-10-04 19:27:07

回答

0

單引號(')表示字符文字。方括號([])表示列名稱。所以,你的看法創作應該是這樣的:

CREATE VIEW CarRentalHistoryView 
AS 
SELECT c.vehNo, c.brand, c.dateRegistered, c.purchasePrice, c.rentalCost, 
     c.condition as [Before Condition], 
     r.condition as [AfterC ondtion],  
     r.dateRented, r.dateReturned, r.CustNo, 
     cu.name, cu.id, cu.dateOfBirth 
FROM dbo.Car c 
Inner JOIN dbo.Rental AS r ON c.VehNo = r.VehNo 
Inner JOIN dbo.Customer as cu on cu.CustNo = r.CustNo ; 
GO 

現在,你可以適當地查詢它:

SELECT * 
FROM dbo.CarRentalHistoryView 
WHERE [Before Condition] != [After Condition] 
+0

Tks,這是整理出來的。 – JackyBoi 2014-10-04 19:59:29