以下是我的查询 -
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”附近的语法不正确。
你能帮我纠正一下语法吗?
改变如下。如果在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
在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
您需要在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);