'附近的语法不正确。'在SQL中

时间:2016-06-20 20:08:19

标签: sql sql-server-2012

我试图通过在我之前制作的数据库上创建视图来进行SQL查询。在查看具有联接的表之前它工作正常,但是现在当我尝试创建一个视图时,我得到一个"不正确的语法接近'。'"来自第一个INNER JOIN语句的错误消息。为什么会这样?

CREATE VIEW vm_order_cost
AS
SELECT  orders.order_id,
    orders.order_date,
    products.product_id,
    customers.name,
    order_cost = (order_details.quantity * products.unit_price)
FROM orders
    INNER JOIN customers AS orders.customer_id = customers.customer_id
    INNER JOIN order_details AS orders.order_id = order_details.order_id
    INNER JOIN products AS order_details.product_id = products.product_id
WHERE order_id BETWEEN '10000' AND '10200'
GO

1 个答案:

答案 0 :(得分:4)

您应该使用ON代替AS

CREATE VIEW vm_order_cost
AS
SELECT  orders.order_id,
    orders.order_date,
    products.product_id,
    customers.name,
    order_cost = (order_details.quantity * products.unit_price)
FROM orders
    INNER JOIN customers ON orders.customer_id = customers.customer_id
    INNER JOIN order_details ON orders.order_id = order_details.order_id
    INNER JOIN products ON order_details.product_id = products.product_id
WHERE order_id BETWEEN '10000' AND '10200'