SQL:[table_name] AS [alias_name]给出错误“找不到存储过程'table_name'”

时间:2018-04-30 14:16:41

标签: sql sql-server

我正在尝试创建一个表别名,以使我的SQL代码更具可读性。目前我有一个很长的表名,我想给别名。不确定它是否有所作为,但我只对我正在尝试为其创建别名的表具有读取权限。我可以通过其他方式完成我想做的事情吗?

编辑: EDIT2(“订单”未改为“orderName”的小错误)

ALL_THE_CUSTOMER_NAMES AS NAMES
ALL_THE_PRODUCTS_IN_THE_STORE AS PRODUCTS

SELECT 
    NAMES.meaning AS meaning,
    PRODUCTS.orderName AS orderName,
    PRODUCTS.publishedDate AS oldDate, 
    PRODUCTS.dueDate AS paymentDate, 
    COUNT(*) AS numberCount, 
    AMOUNT AS AMOUNT,
    DATEDIFF(d, PRODUCTS.publishedDate, PRODUCTS.dueDate) as dateDifference,
    orderType as Fordringstype
FROM PRODUCTS
INNER JOIN NAMES
on PRODUCTS.ID = NAMES.ID
WHERE 
    (PRODUCTS.customerID = 1 
    or 
    PRODUCTS.customerID = 2)
    and
    PRODUCTS.orderType = 'CARS'
    and
    PRODUCTS.orderName NOT IN ('BLUE', 'RED')
GROUP BY 
    PRODUCTS.orderName,
    PRODUCTS.publishedDate,
    PRODUCTS.dueDate,
    PRODUCTS.AMOUNT,
    PRODUCTS.orderType,
    NAMES.meaning
Order BY PRODUCTS.publishedDate

2 个答案:

答案 0 :(得分:1)

您不会将表的别名放在SQL的顶部; SQL不像声明性语言那样工作。就像我在评论中说的那样:

  

您使用AS子句对表格进行别名,就像使用SELECT

中的列一样

所以,正确的语法是:

SELECT 
    NAMES.meaning AS meaning,
    PRODUCTS.orderName AS orderName,
    PRODUCTS.publishedDate AS oldDate, 
    PRODUCTS.dueDate AS paymentDate, 
    COUNT(*) AS numberCount, 
    AMOUNT AS AMOUNT,
    DATEDIFF(d, PRODUCTS.publishedDate, PRODUCTS.dueDate) as dateDifference,
    orderType as Fordringstype
FROM ALL_THE_PRODUCTS_IN_THE_STORE AS PRODUCTS
INNER JOIN ALL_THE_CUSTOMER_NAMES AS NAMES ON PRODUCTS.ID = NAMES.ID
WHERE 
    (PRODUCTS.customerID = 1 
    or 
    PRODUCTS.customerID = 2)
    and
    PRODUCTS.orderType = 'CARS'
    and
    PRODUCTS.orderName NOT IN ('BLUE', 'RED')
GROUP BY 
    PRODUCTS.orderName,
    PRODUCTS.publishedDate,
    PRODUCTS.dueDate,
    PRODUCTS.AMOUNT,
    PRODUCTS.orderType,
    NAMES.meaning
Order BY PRODUCTS.publishedDate;

有任何问题,请问。

答案 1 :(得分:0)

使用以下语法:

Select columnname from tablename alias...