【发布时间】:2012-07-30 01:52:20
【问题描述】:
我正在尝试使用 CTE 中包含的数据更新表。不幸的是,我收到了一个语法错误,我不太清楚为什么。目前的代码是:
declare @period_id integer =
(
select period_id
from property.period
where getdate() between period_start and period_end
)
;with cte_reclassified as
(
select building_id ,
lease_id ,
scca_broad_category_code ,
scca_fine_categories_code ,
scca_notes_code ,
scca_sales_group_code ,
scca_uplift
from property.lease_period
where period_id = @period_id
)
update property.lease_period lp
from cte_reclassified r
set lp.scca_broad_category_code = r.scca_broad_category_code
where lp.lease_id = r.lease_id
and lp.building_id = r.building_id
我收到的语法错误是:
消息 102,级别 15,状态 1,第 21 行 'lp' 附近的语法不正确。
有没有办法做我在这里尝试的事情?我试过用谷歌搜索这个主题,但遇到了死胡同 - 任何建议都将不胜感激!
【问题讨论】:
-
您确定您完全按照原样粘贴了吗?我想知道这是因为字母“
lp”没有出现在代码中的任何地方 -
@AdamBatkin 哎呀!为 s/oflow 削减它的副产品。现在更新。编辑:固定!
-
我认为 SQL Server 不允许您在更新语句中指定别名。只需删除别名并将出现的地方替换为表名即可。
标签: sql sql-server-2008 tsql common-table-expression