无法使用查询创建表

时间:2014-10-04 19:25:55

标签: sql tsql

我正在尝试编写一个从此视图中选择的查询。

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!

1 个答案:

答案 0 :(得分: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]