Sql - SQL错误[156] [S1000]:关键字'as'附近的语法不正确

问题描述 投票:-2回答:3

以下是我的查询 -

DELETE
FROM activity_details as ad, edit_details as ed
LEFT JOIN edit_details as ed
ON ed.details_id = ad.row_id
WHERE year(ad.activity_datetime)  = year(GETDATE())-10

我收到错误SQL错误[156] [S1000]:关键字'as'附近的语法不正确。 SQL错误[156] [S1000]:关键字“as”附近的语法不正确。

你能帮我纠正一下语法吗?

sql-server mssql-jdbc
3个回答
3
投票

改变如下。如果在delete语句中使用alias,则需要使用FROM名称删除。

DELETE ad
FROM activity_details as ad
LEFT JOIN edit_details as ed
ON ed.details_id = ad.row_id
WHERE year(ad.activity_datetime)  = year(GETDATE())-10

1
投票

delete之后添加别名

 DELETE ad
    FROM activity_details as ad
    LEFT JOIN edit_details as ed
    ON ed.details_id = ad.row_id
    WHERE year(ad.activity_datetime)  = year(GETDATE())-10

1
投票

您需要在delete子句中使用别名,然后使用FROM。我还建议不要使用year(ad.activity_datetime)并使用SARGable查询:

DELETE ad
FROM activity_details ad
     LEFT JOIN edit_details ed ON ed.details_id = ad.row_id
WHERE ad.activity_datetime >= DATEADD(YEAR,DATEDIFF(YEAR, 0, GETDATE()-10),0)
  AND ad.activity_datetime < DATEADD(YEAR,DATEDIFF(YEAR, 0, GETDATE()-9),0);
© www.soinside.com 2019 - 2024. All rights reserved.